I got little project in VBA and stuck on below topic
I need to Sum selected range in first empty cell in B column. I tried a small macro in which it sums the same row which mentioned in the vba.
This is what I've found and try to use
Sub Macro9()
'
' Macro9 Macro
'
'
Range("A3").Select
Selection.Copy
Range("A2").Select
ActiveSheet.Paste
Range("B2").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=+SUM(R[1]C:R[3]C)"
Range("B2").Select
Selection.Copy
Range("B3").Select
Selection.End(xlToRight).Select
Range("S2").Select
Range(Selection, Selection.End(xlToLeft)).Select
ActiveSheet.Paste
Selection.End(xlToLeft).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Final.xlsx").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False
Range("A6").Select
Windows("copy.xlsm").Activate
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Selection.End(xlToLeft).Select
End Sub
I tried to searched for last non-empty cell in selected range so it won't search the whole column
To get the sum of all the non empty cells in Column "B" and return it to Column "B2" as I can understand with the image, following is the code which help you:
Range("B2").FormulaR1C1 = "=Sum(R3C:R" & ActiveSheet.UsedRange.Rows.Count + 1 & "C)"
You may use following code to get the sum of till first empty cell:
Range("B2").FormulaR1C1 = "=Sum(R3C:R" & Range("B3").End(xlDown).Row & "C)"
use SpecialCells method of Range object to get not empty cells in a given range
then use Areas property of Range object to loop through each group of consecutive not empty cells
Option Explicit
Sub SumThem()
Dim c As Range
For Each c In Intersect(ActiveSheet.UsedRange, Columns(2)).Offset(1).SpecialCells(xlCellTypeConstants).Areas ' loop through each "area" (i.e.: group of consecutive cells with "constant" values) of column B from row 2 downwards
c.Resize(1).Offset(-1, 0).Formula = "=+SUM(" & c.Address & ")" ' place the sum of current "area" in the cell right above it
Next
End Sub
Related
I want to merge the first and the last row using the =cell1&cell2 function of the table but was unable to as the number of row can be dynamic.
Tried using the relative distance using ctrl+up but to no avail.
Ideally a VBA code where I can use the "&" function to merge the most top and last row of the table then paste special on top as text
Sub Macro9()
ActiveCell.FormulaR1C1 = "=R[-9]C&R[-2]C"
ActiveCell.Select
Selection.Copy
Selection.End(xlUp).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(2, 0).Range("A1").Select
Range(Selection, Selection.End(xlToLeft)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
If I understand correctly , what you want is a way to address the last cell in a column.
You can do this as follows:
Set sht = Sheets("main")
column = 1
lastRow = sht.Cells(sht.Rows.Count, column).End(xlUp).Row
Set lastCell = sht.Cells(lastRow, column)
lastCell is a range variable referencing the last cell in the column specified by the column variable. I explicitly referenced the sheet to avoid problems with active sheets.
Sub MergeCells()
col = 1 // column A
lastRow = Cells(Rows.Count, 1).End(xlUp).row
Mcell = Cells(1, col) & Cells(lastRow, col)
End Sub
I have to transpose rows to columns in excel using vba and the data is of around 500000.
The problem is that the data is not regular and is consistent.
Like there will be 4 rows then a blank then it can be three rows or one as well.
I want to transpose the group of data separated by a blank cell to be transposed to the the respective column in-front of the first entry.
Sub Transpose()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Loop
End Sub
I used this code but the problem is that it is skipping the data which is present in the single row.
Then this should do it, beware that I'm assuming where your data is and where is going to get paste, don't forget to change that:
Option Explicit
Sub Transpose()
Dim LastRow As Long 'last row on the sheet
Dim TransposeRow As Long 'row where we transpose
Dim x As Long 'columns
Dim C As Range 'faster looping through cells with For Each C in range
With ThisWorkbook.Sheets("MySheet") 'change this to your sheet
'To assign the last row im gonna assume your data is in column A or 1(B would be 2 and so...)
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'Last row with data
TransposeRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1 'on column B will be pasting the data
x = 2 'initialize x being 2 as for B column
For Each C In .Range("A2:A" & LastRow)
If C = vbNullString Then 'in case the cell is blank we jump a row
TransposeRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1 ' recalculate row for transposing data
x = 2 'reinitialize column counter
Else
.Cells(TransposeRow, x) = C 'we copy the value to the row and column empty
x = x + 1 'add 1 column
End If
Next C
End With
End Sub
I have edited your code to show an approach that can work for you. You need to add a condition for one cell data.
Sub Transpose2()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
If IsEmpty(ActiveCell.Offset(1, 0).Value) Then
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Else
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
End If
Application.CutCopyMode = False
Selection.End(xlDown).Select
Loop
End Sub
Note: Using select is not generally a good idea. An example of cutting down select would be:
Sub Transpose3()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
If IsEmpty(ActiveCell.Offset(1, 0).Value) Then
ActiveCell.Copy ActiveCell.Offset(0, 1)
Else
Range(ActiveCell, ActiveCell.End(xlDown)).Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").End(xlDown).Select
End If
Application.CutCopyMode = False
Selection.End(xlDown).Select
Loop
End Sub
I am having some issues with my VBA code that will select data from one sheet, copy it, paste it to a new sheet and insert a total at the bottom of the table. The first steps work, but I am struggling with the total , any help would be greatly appreciated. Here is what I have so far:
Range("A3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "='Pricing Main'!RC[1]"
Range(xlToRight, xlDown).Offset(1, 0).Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=SUM(R[-36]C:R[-1]C)"
End Sub
If the problem is in making formula with dynamic range (while first row is always the same and the 2nd is different), you could use:
ActiveCell.FormulaR1C1 = "=SUM(R1C:R[-1]C)"
If you're using number without "[]" it will absolute address with $
In this example it will be ="SUM(A1:Ax)" where X is row before active cell
Another option is using Activecell.Formula and get this address by combining letters with Activecell.row
activecell.Formula = "=SUM(A1:A" & activecell.Row - 1 &")"
And after that you can autofill to other columns if needed by
activecell.AutoFill Destination:=range(activecell, activecell.Offset(0,5)), Type:=xlfilldefault
3rd option could be using range variables calculate address before formula:
Sub Makro1()
Dim rng1 As Range Dim rng2 As Range Dim rng3 As Range
Set rng1 = Range("A1") Set rng2 = ActiveCell.Offset(-1, 0) Set rng3 = Range(rng1, rng2)
ActiveCell.Formula = "=SUM(" & rng3.Address & ")"
End Sub
If you have another problem, please, try explain it better ;)
I need a macro to append a comma onto the beginning of a column of text strings. I recorded the action myself, but it limited itself to Column C (often, the text strings I need to do this with appear in a different column), and also limited the application of the range to the specific number of rows in the worksheet I recorded it on (in this case, 114).
Here is the original Record Macro output:
Sub AddCommaToESIID()
'
' AddCommaToESIID Macro
'
'
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("E:E").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C2").Select
ActiveCell.FormulaR1C1 = ","
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C114")
Range("C2:C114").Select
Range("E2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]&RC[-1]"
Range("E2").Select
Selection.AutoFill Destination:=Range("E2:E114")
Range("E2:E114").Select
Selection.Copy
Range("D2").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Columns("E:E").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
End Sub
I would like to modify this to achieve the following:
Make the macro apply to whatever Column I have selected, as opposed to Column C
Once I have selected a Column, make the macro apply to however many rows there are in the particular worksheet I'm working on.
Thanks in advance for your help!
This will change all non-formulas in whatever range you select
Sub AddCommaToESIID()
Dim rCell As Range
If TypeName(Selection) = "Range" Then
For Each rCell In Selection.Cells
If Not rCell.HasFormula Then
rCell.Value = "," & rCell.Value
End If
Next rCell
End If
End Sub
I have 2 workbooks Say Workbook 1 and workbook 2. Need to put a filter in Workbook 2 with filter "INTDN" in column G and copy column "O" and column "J" in workbook 1, column "B" and Column "I" respectively.
Can any one give a VBA code for this?
Workbook and worksheet name can be anything depending on the source however format will be same always.
Some more information for your reference:-
Workbook 2
Put a filter in Row 12 column G :- "INTDN"
Workbook 1
Paste copied data from column "O" to cell B25 downwards.
Paste copied data from column "J" to cell I25 downwards.
I will then assign this Macro to every worksheet I need this to work.
I am a novice in VBA. Appreciate your assistance.
This is what I could write:-
Sub CopyData()
'
' CopyData Macro
'
'
Windows("Book1 (8).xlsx").Activate
Range("A12").Select
Selection.AutoFilter
Range("G12").Select
ActiveSheet.Range("$A$12:$AV$72").AutoFilter Field:=7, Criteria1:="INTDV"
Range("O35").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("DebitNotes.xlsm").Activate
ActiveSheet.Paste
Range("I25").Select
Windows("Book1 (8).xlsx").Activate
Range("J35").Select
Range(Selection, Selection.End(xlDown)).Select
Range("J35:J72").Select
Application.CutCopyMode = False
Selection.Copy
Windows("DebitNotes.xlsm").Activate
ActiveSheet.Paste
End Sub
This code will copy values from column O on sheet1 and copy to end of column P in sheet2.
I also made it generic so it can be used for any columns and sheets.
Sub Test()
Call CopyColumn("Sheet1", "O", "Sheet2", "P")
End Sub
Function CopyColumn(sourceSheetName As String, sourceColIndex As String, destSheetName As String, destColIndex As String)
Dim lastRowSource As Integer: lastRowSource = Sheets(sourceSheetName).Cells(Rows.Count, sourceColIndex).End(xlUp).Row
Dim lastRowDest As Integer: lastRowDest = Sheets(destSheetName).Cells(Rows.Count, destColIndex).End(xlUp).Row
Sheets(sourceSheetName).Range(sourceColIndex & "1:" & sourceColIndex & lastRowSource).Copy Destination:=Sheets(destSheetName).Range(destColIndex & lastRowDest + 1)
End Function
I have this final code which is working fine. Only problem is that I want this code to work for any open workbook>worksheet. Name of the workbook or worksheet can be anything. It is not in my control.
Sub CopyPaste()
'
' CopyPaste Macro
'
'
Range("H11").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Range("H12").Select
Windows("Data1.xlsx").Activate
Range("A12").Select
Selection.AutoFilter
Range("G12").Select
ActiveSheet.Range("A12").AutoFilter Field:=7, Criteria1:="INTDV"
Range("O35").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("DebitNotes.xlsm").Activate
Range("B25").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("I25").Select
Windows("Data1.xlsx").Activate
Range("J35").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("DebitNotes.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("I207").Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.ClearContents
Range("I207").Select
Columns("I:I").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
Range("I207").Select
End Sub