我使用以下VBA代码复制Sheet1中的范围并将其粘贴到同一张纸中。但是我需要将数据粘贴到sheet2的下一个可用行中。
Private Sub CommandButton1_Click()
Sheets("Sheet1").Range("A1:A5").Copy
Dim lastrow As Long
lastrow = Range("A65536").End(xlUp).Row
Sheets("Sheet2").Activate
Cells(lastrow + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
请帮帮我..
答案 0 :(得分:1)
试试这个:
Private Sub CommandButton1_Click()
Dim lastrow As Long
Dim rng1 As Range
Dim rng2 As Range
lastrow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
Set rng1 = Sheets("Sheet1").Range("A1:A5")
Set rng2 = Sheets("Sheet2").Range("A" & lastrow + 1)
rng1.Copy
rng2.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
答案 1 :(得分:0)
您的代码很好但需要更改一行,即{/ 1}}行之前 Sheets("Sheet2").Activate
lastrow = Range("A65536").End(xlUp).Row
答案 2 :(得分:0)
首先激活sheet2,然后找到最后一行
lastrow = Range("A65536").End(xlUp).Row