0

我在尝试编写 VBA 代码以将列的内容复制到同一项目中的另一个工作表时遇到了问题。问题在于,它不是复制整个列的数据,而是只复制最后一行的数据。

这是我拥有的 VBA 代码。提前致谢。

Sub copycolumns()
Dim lastrow As Long, erow As Long

lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow 'this will iterate from the 2nd row, as first one is the header

    Sheet1.Cells(i, 1).Copy 'copy value of first row i and column one
    erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

    Sheet1.Paste Destination:=Worksheets("Sheet4").Cells(erow, 1)
    'paste it on Sheet4's first empty row and column 1.

    Sheet1.Cells(i, 4).Copy 'copy value of first row i and column 4
    Sheet1.Paste Destination:=Worksheets("Sheet4").Cells(erow, 3)
    'paste it on Sheet4's first empty row and column 3
    
    Sheet1.Cells(i, 5).Copy 'copy value of first row i and column 5
    Sheet1.Paste Destination:=Worksheets("Sheet4").Cells(erow, 2)
    'paste it on Sheet4's first empty row and column 2
    
    Sheet1.Cells(i, 8).Copy 'copy value of first row i and column 8
    Sheet1.Paste Destination:=Worksheets("Sheet4").Cells(erow, 4)
    'paste it on Sheet4's first empty row and column 4

    Sheet1.Cells(i, 9).Copy 'copy value of first row i and column 9
    Sheet1.Paste Destination:=Worksheets("Sheet4").Cells(erow, 5)
    'paste it on Sheet4's first empty row and column 5
        

Next i 'go to the next row

'will implement this clean up commands after code works
    'Application.CutCopyMode = False
    'Sheet2.Columns.AutoFit
    'Range("A1").Select
    
End Sub
4

0 回答 0