Copy all the columns vertically in a spreadsheet

后端 未结 1 667
孤街浪徒
孤街浪徒 2021-01-27 12:05
set ws1 As SheetA
set ws2 As Target  
With ws1
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
Lastrow = ws1.Range(\"B\" & Rows.Count).End(xlUp).Ro         


        
相关标签:
1条回答
  • 2021-01-27 12:42

    If the Lastrow for every column is the same (referenced to B Column) then you can try below:

    With ws1
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
    End With
    
    Dim i As Long, LastRow2 As Long
    For i = 0 To LastCol - 1
        With ws2
            LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            ws1.Range("A4:A" & LastRow).Offset(0, i).Copy .Range("A" & LastRow2)
        End With
    Next
    

    You don't need to copy each cell one by one by doing nested For Loop.
    You can set the first group of cell to copy (Range("A4:A" & LastRow)), then just use Offset to copy the next column using just one(1) loop.

    If LastRow for every column is different, you can try:

    Dim LastCol As Long, LastRow As Long
    LastCol = ws1.Cells(1, ws1.Columns.Count).End(xlToLeft).Column
    
    Dim i As Long, LastRow2 As Long
    For i = 0 To LastCol - 1
        LastRow = ws1.Cells(ws1.Rows.Count, i + 1).End(xlUp).Row
        With ws2
            LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            ws1.Range("A4:A" & LastRow).Offset(0, i).Copy .Range("A" & LastRow2)
        End With
    Next
    
    0 讨论(0)
提交回复
热议问题