我是初学者,我在下面有这个代码,但这只是从工作表复制最后一行并粘贴到工作表2范围。基本上空行没有得到更新。并且还抛出运行时错误1004 - 应用程序定义或对象定义错误。任何帮助将不胜感激。
Sub copypaste()
Dim lastrow As Long, erow As Long
lastrow = ThisWorkbook.Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
Sheet1.Cells(i, 3).Copy
erow = ThisWorkbook.Worksheets("sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 2)
Sheet1.Cells(i, 14).Copy
Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 4)
Sheet1.Cells(i, 6).Copy
Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 3)
Next i
Application.CutCopyMode = False
ThisWorkbook.Worksheets("sheet2").Columns().AutoFit
Range("A1").Select
End Sub
让我们说这是第1单 sheet 1
这是sheet2: sheet2:
标题在命名方面略有不同,并且还有位置更改。第1页的最后一行是多种多样的。
答案 0 :(得分:1)
试试这个,总是更好地避免复制和粘贴。
Sub copypaste()
Dim lastrow As Integer, erow As Integer, sheet1 As Worksheet, sheet2 As Worksheet
Set sheet1 = Worksheets("Sheet1")
Set sheet2 = Worksheets("Sheet2")
lastrow = sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
erow = sheet2.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
sheet2.Cells(erow, 2) = sheet1.Cells(i, 3)
sheet2.Cells(erow, 3) = sheet1.Cells(i, 6)
sheet2.Cells(erow, 4) = sheet1.Cells(i, 14)
Next i
'ThisWorkbook.Worksheets("Sheet2").Columns().AutoFit
'sheet1.Cells(1, 1).Activate
End Sub