Knockoutpie
Board Regular
- Joined
- Sep 10, 2018
- Messages
- 116
- Office Version
- 365
- Platform
- Windows
HI all, I'm pasting the copied data to the bottom row of WB2 set below but receiving Error 1004, can someone please help figure out what is not coded correctly?
VBA Code:
Private Sub CommandButton2_Click()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Name workbook we are copying data from
Dim WB1 As Workbook
Set WB1 = ActiveWorkbook
Worksheets("row").Activate
' Copy all data from row
Dim rng As Range
Set rng = ActiveSheet.UsedRange
Set rng = rng.Offset(1).Resize(rng.Rows.Count - 1)
rng.Copy
'name the workbook
Dim WB2 As Workbook
Set WB2 = Workbooks.Open("C:\Users\User1\Documents\Reports\Splits\Sales comm2022.xlsx")
' Paste the data from last file to bottom of the sheet
Worksheets("Consolidate - Jan to Sep 22").Activate
Dim lastRow As String
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Range("A" & lastRow).Select ' receive error here
Selection.PasteValues
' Close WB2 (last month)
WB2.Close
' Go back to original workbook
WB1.Activate
End Sub