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).Row
For i = 1 To Lastcol
For l = 4 To lastrow
ws1.Cells(l, i).Copy ws2.Range("a65536").End(xlUp).Offset(1, 0)
Next l
Next i
我正在尝试将一张纸中的所有列复制到另一张纸的下方。基本上将 Sheet1 的 A 列复制到 Sheet2 的 A 列,然后将 Sheet1 的 B 列复制到 Sheet2 的 A 列下方。
每列中的数据从工作表 A 的第 4 行开始,因此我保留了第二行For Loop
从 4 开始运行。非常感谢任何帮助,因为我现在被困了几个小时。当我运行代码时,它会进入无限循环。
If the Lastrow
每列都是相同的(参考B
列)然后你可以尝试以下:
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
您不需要通过嵌套来逐个复制每个单元格For Loop
.
您可以设置要复制的第一组单元格(Range("A4:A" & LastRow)
),然后只需使用Offset
仅使用一(1)个循环复制下一列。
If LastRow
对于每列都不同,您可以尝试:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)