Error 1004 when pasting to bottom row of workbook

Knockoutpie

Board Regular
Joined
Sep 10, 2018
Messages
116
Office Version
  1. 365
Platform
  1. 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
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
What looks wrong to me: last row variable should be a long or integer, not string. I'd try fixing that first.
 
Upvote 0
What looks wrong to me: last row variable should be a long or integer, not string. I'd try fixing that first.
Thanks, since i'm using multiple workbook the issue was using .cells.
I ended up changing lastrow section to the following by declaring the sheet name

VBA Code:
    With Sheets("Consolidate - Jan to Sep 22").Range("A" & Rows.Count).End(xlUp).Offset(1)
        .PasteSpecial (xlPasteAll)
        .PasteSpecial (xlPasteValues)
    End With

More detailed reason why can be found here

 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top