I am quite new to VBA, hence unable to understand the scripts at this moment.
I tried recording a macro and it does quite a good job. However, it's not dynamic.
Here is my use case:
I have an excel workbook and it has two sheets named "Sheet1" & "FinalData". All I want to do is copy the data from a specific cell range of let's say C2:P2 from "sheet1" and append it to the "FinalData" sheet.
Basically, find the last empty row and paste the data there. Below is my recorded piece of vba code that indicates this function.
Can anyone help me with fixing the below code or sharing a new code, please? I will be grateful to you.
Thanks!
Application.CutCopyMode = False
Range("C2").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("FinalData").Select
Range("A2").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Try this code, to select the last empty row, just sum + 1 to the total used rows
Application.CutCopyMode = False
Range("C2").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("FinalData").Select
Cells(Sheets("FinalData").UsedRange.Rows.Count + 1, 1).Select ' Select last empty row
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sub copyData()
Dim sheet1 As Worksheet
Dim finalDataSheet As Worksheet
Set sheet1 = Sheets("Sheet1")
Set finalDataSheet = Sheets("FinalData")
'Get the last column based on the second row.
lastcolumn = sheet1.Cells(2, Columns.Count).End(xlToLeft).Column
'Get last row based on column C.
lastrow = sheet1.Cells(Rows.Count, 3).End(xlUp).Row
'Option A:
'Only transfer row 2 with dynamic columns from C to end column.
finalDataSheet.Range(finalDataSheet.Cells(2, 3), finalDataSheet.Cells(2,
lastcolumn)).Value = _
sheet1.Range(sheet1.Cells(2, 3), sheet1.Cells(2, lastcolumn)).Value
'Option B:
'Transfer entire dynamic table structure starting at c2.
'Get the values from sheet 1 data range and copy values over to final data
sheet into the same position.
finalDataSheet.Range(finalDataSheet.Cells(2, 3),
finalDataSheet.Cells(lastrow, lastcolumn)).Value = _
sheet1.Range(sheet1.Cells(2, 3), sheet1.Cells(lastrow, lastcolumn)).Value
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 a table in Sheet1 of a workbook and several rows of the table will have #N/A as their value of column N. I would like to find a way to have a vba macro find all rows that have #N/A in column N then copy the values from column M and L of those rows to the bottom of another table on Sheet2 of the same workbook.
ActiveSheet.ListObjects("SEC_Data").Range.AutoFilter Field:=14, Criteria1:= _
"#N/A"
Range("M88343:M88351").Select
Selection.Copy
Sheets("LKUP_Client Name").Select
Range("B2").Select
Selection.End(xlDown).Select
Range("B" & ActiveCell.Row + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("company_2018 thru2019_gim").Select
Range("L88343:L88351").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("LKUP_Client Name").Select
Range("C").Select
Selection.End(xlDown).Select
Range("C" & ActiveCell.Row + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
The way I would approach this is to first iterate through column N on sheet 1, when #N/A found then copy the cells and paste in corresponding location on sheet 2. Something like the below:
Sub CopyProcedure()
Dim i As Long
Dim lRow1 As Long, lRow2 As Long
Dim wsSheet1 As Worksheet, wsSheet2 As Worksheet
Set wsSheet1 = Sheets("Sheet 1")
Set wsSheet2 = Sheets("Sheet 2")
lRow1 = wsSheet1.Range("N" & wsSheet1.Rows.Count).End(xlUp).Row
'assuming your data starts in the first row
'iterate to the last row of column n
For i = 1 To lRow1
'look for the #N/A text
If wsSheet1.Range("N" & i).Text = "#N/A" Then
'adjust this to suit which column in sheet 2 you need
lRow2 = wsSheet2.Range("A" & wsSheet2.Rows.Count).End(xlUp).Row + 1
'when text found copy required cells
wsSheet1.Range("L" & i, "M" & i).Copy
'paste cell values in required location on sheet 2
'NOTE THIS WILL PASTE IN THE LAST ROW SPECIFIED ON SHEET 2 AND IN COLUMN A
'adjust as you see fit
wsSheet2.Range("A" & lRow2).PasteSpecial xlPasteValues
'empty clipboard
Application.CutCopyMode = False
End If
Next i
Set wsSheet1 = Nothing
Set wsSheet2 = Nothing
End Sub
This is by no means the most efficient way to do it, but I am sure it will get the job done if I understand your problem correctly.
Also, caveat, I haven't tested or debugged this. :)
Dim i, last As Integer
last = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To last
If Cells(i, 1) <> "" Then
Range("A1:C").Select
Selection.Copy
Sheets("Sheet2").Select
Application.Run "updatecc"
Range("A1:c").Select
Selection.Insert Shift:=xlDown
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
Application.Run "updatecc"
End If
Next i
End Sub
Help needed here, I want to add cells dependent upon data in cells of sheet1 to sheet2.
It is copying only first a1:c1 values.
I need to copy from sheet1 and paste it in sheet2 and generate cells.
Replace the first Range("A1:C").Select by Range(Cells(i,1),Cells(i,3))
This will select columns A to C of the row that you are checking.
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
In my macro, i need to insert a formula in columns AA and AB, AB is empty so no problem there. Column AA has data which starts in AA10954, my problem is AA10954 changes each week as my data either increases or decrease as i import from another book, can someone help me to set the last empty cell before my data starts in AA?
Sub ClassVisit()
'
' ClassVisit Macro
'
'
Dim lr As Long
With ActiveWorkbook
With ActiveSheet
lr = .Cells(.Rows.Count, "A").End(xlUp).Row
Range("AA2").Formula = "=IFERROR(VLOOKUP(A2,[Data.xlsb]Stores!$A:$AA,27,0),VLOOKUP(A2,'[Salesinfo.xlsb]Packs'!$C:$E,3,0))"
Range("AB2:AB" & lr).Formula = "=IFERROR(VLOOKUP(A2,Attribute!D:F,3,0),""Not Visited"")"
Range("AA2").Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A10953")
ActiveCell.Range("A1:A10953").Select
Range("AA2:AB" & lr).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.End(xlUp).Select
Application.CutCopyMode = False
ActiveCell.Offset(1, -26).Range("A1").Select
End With
End With
End Sub