我正在使用VBA将一些数据从一张纸粘贴到另一张纸上。
按下第一个按钮的工作原理是(更新行i45等)。
按下第二个按钮是为了将数据粘贴到下一行(i46等等),因为它是空行)但它实际上更新了行i21任何后续按下只是更新i21覆盖。
请你检查一下我做错了什么。
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("Raw")
Set pasteSheet = Worksheets("Week Data (all)")
copySheet.Range("H15:AA15").Copy
pasteSheet.Cells(45, 9).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:1)
这是因为pasteSheet.Cells(45, 9)
您总是在第45行开始搜索。相反,您应该在.Rows.Count
开始搜索,这是您列中绝对的最后一行(即.1048576)。因此,试试......
With pasteSheet
.Cells(.Rows.Count, 9).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End With
答案 1 :(得分:0)
这个方法也一样。
Private Sub CommandButton1_Click()
Dim vDB
Dim rngT As Range
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Application.ScreenUpdating = False
Set copySheet = Worksheets("Raw")
Set pasteSheet = Worksheets("Week Data (all)")
vDB = copySheet.Range("H15:AA15")
Set rngT = pasteSheet.Cells(Rows.Count, 9).End(xlUp)(2)
rngT.Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB
Application.ScreenUpdating = True
End Sub