简体   繁体   中英

VBA Paste Data After Last Row Issue

I am compiling data from multiple worksheets to place on one consolidated sheet. The first sheet is easy because I can paste the entire sheet however after that it becomes tricky as I only need from A5-H### as I no longer need the header. I then need to paste this information at the bottom of the previous paste. I am getting a Range of Object "_Global" failed for the 'Range(lastRow).Select'. The issue is when I look at it in the debugger it is coming up with the correct row number. What am I doing wrong?

Sheets(1).Select
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Consolidation Sheet").Select
Range("A1").Select
ActiveSheet.Paste
Sheets(2).Select
Range("A5:A925").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Consolidation Sheet").Select
Range("A5").Select
lastRow = ActiveSheet.Cells(Rows.Count, "E").End(xlUp).Row + 1
Range(lastRow).Select
ActiveSheet.Paste

You need your Range statement in the following form (for example):

Range("A1").Select 

So since you've got the row number ( lastRow ), you just need to add the column reference too:

Range("E" & lastRow).Select

Extra info

Avoid using Select by referring directly to the worksheets/ ranges you want to deal with

Sheets(1).Cells.Copy Destination:=Sheets("Consolidation Sheet").Range("A1")

With Sheets(2)
    .Range(.Range("A5:A925"),.Range("A5:A925").End(xlToRight)).Copy
End With

With Sheets("Consolidation Sheet")
    .Cells(.Rows.Count, "E").End(xlUp).Offset(1,0).PasteSpecial
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM