[英]Excel VBA - Copy range from one sheet to another, next empty row
I'm trying to take a range from one sheet and copy it to the next empty row in another sheet (basically, paste into the range A12:D12
for next empty row in the other sheet). 我正在尝试从一个工作表获取一个范围,并将其复制到另一工作表中的下一个空行(基本上,将其粘贴到另一工作表中下一个空行的范围A12:D12
中)。 The range will never change. 范围永远不会改变。 I've seen a lot of questions like this, with people saying the answers work great, but I can't get it to work. 我已经看到很多类似的问题,有人说答案很有效,但我无法解决。
Very new to VBA. VBA的新手。 Here is the code I'm using: 这是我正在使用的代码:
Private Sub CommandButton1_Click()
Dim NextRow As Range
Set NextRow = Range("A" & Sheets("Sheet3").UsedRange.Rows.Count + 1)
Sheet1.Range("A12:D12").Copy
Sheet3.Activate
NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
This runs but it doesn't actually paste any values into Sheet3
. 这会运行,但实际上不会将任何值粘贴到Sheet3
。 Is there something I'm missing? 有什么我想念的吗? Is there a better way to do this? 有一个更好的方法吗?
Thanks! 谢谢!
You just had an issue in the second line defining NextRow
. 您在定义NextRow
的第二行中遇到了一个问题。
Is there a better way to do this? 有一个更好的方法吗? It depends on your needs; 这取决于您的需求; personally I do not like to activate/select other cells during a VBA macro so eg I would get rid of the Sheet3.Activate
. 我个人不喜欢在VBA宏中激活/选择其他单元格,因此例如,我将摆脱Sheet3.Activate
。 I would also copy the stuff 'manually' without using the clipboard to avoid changing the user's clipboard contents. 我也将“手动”复制内容而不使用剪贴板,以避免更改用户的剪贴板内容。
Private Sub CommandButton1_Click()
Dim NextRow As Range
Set NextRow = Sheet3.Range("A" & Sheet3.Rows.Count).End(xlUp).Offset(1, 0)
Sheet1.Range("A12:D12").Copy
Sheet3.Activate
NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.