最后一行问题后的 VBA 粘贴数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43057655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Paste Data After Last Row Issue
提问by Greg Herr
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?
我正在编译来自多个工作表的数据以放置在一个合并的工作表上。第一张工作表很容易,因为我可以粘贴整个工作表,但是之后它变得棘手,因为我只需要 A5-H###,因为我不再需要标题。然后我需要将此信息粘贴在上一个粘贴的底部。对于“Range(lastRow).Select”,我得到一个对象范围“_Global”失败。问题是当我在调试器中查看它时,它提供了正确的行号。我究竟做错了什么?
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
回答by CallumDA
You need your Range
statement in the following form (for example):
您需要Range
以下形式的声明(例如):
Range("A1").Select
So since you've got the row number (lastRow
), you just need to add the column reference too:
因此,既然您已经获得了行号 ( lastRow
),那么您也只需要添加列引用:
Range("E" & lastRow).Select
Extra info
额外信息
Avoid using Selectby 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