简体   繁体   English

如何在VBA中循环多列

[英]How to loop for multiple columns in VBA

I have this below code to copy data from one sheet and to paste it onto another sheet.我有以下代码可以从一张纸上复制数据并将其粘贴到另一张纸上。 However, i need to paste about 100 columns of data.但是,我需要粘贴大约 100 列数据。 How do i get my loop to run 100 times so i dont have to copy this code down a 100 times?我如何让我的循环运行 100 次,这样我就不必将此代码复制 100 次?

Thanks谢谢

Sub Macro1()

    Dim lastrow as long, erow as long

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

    For i = 2 to lastrow
        Sheets4.cells(i,1).Copy
        erow = Sheet7.Cells(Rows.Count, 1).End(xlUp).Offset(1,0).Row

        Sheet4.Paste Sheet7.Cells(erow,1)

        Sheet4.Cells(i,2).Copy
        Sheet4.Paste Sheet7.Cells(erow,2)

You do not say anything and I cannot stay, anymore... If you do not need to copy format, try the next code.你什么都不说,我不能再呆了……如果你不需要复制格式,试试下一个代码。 It will copy all columns of Sheet4 starting from their second row, to the first empty row of Shee77 .它将Sheet4所有列从第二行开始复制到Shee77的第一个空行。 It is much faster than what you tried, using an array and drop all content at once...它比您尝试的要快得多,使用数组并一次删除所有内容......

Sub Macro1_bis()
 Dim arrSh4 As Variant, sh7Row As Long
 arrSh4 = Sheet4.UsedRange.Offset(1).value
 sh7Row = Sheet7.Range("A" & Cells.Rows.Count).End(xlUp).Row + 1
 Sheet7.Range("A" & sh7Row).Resize(UBound(arrSh4, 1), UBound(arrSh4, 2)).value = arrSh4
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM