VBA:复制和粘贴数据行的最快方法

我想知道是否有更快的方法来复制工作表的一列中的数据和粘贴到工作表的另一列。 这是我有什么样的代码,它运行速度慢,因为它有超过10000行的数据复制每一列,我正在复制。 我从中复制的数据没有固定范围。

For i = 2 To lastrow ws.Cells(i, "AG") = bl.Cells(i, "F") ws.Cells(i, "AH") = bl.Cells(i, "G") ws.Cells(i, "AB") = bl.Cells(i, "N") ws.Cells(i, "AC") = bl.Cells(i, "R") ws.Cells(i, "BF") = bl.Cells(i, "S") ws.Cells(i, "AA") = bl.Cells(i, "U") ws.Cells(i, "BA") = bl.Cells(i, "X") ws.Cells(i, "BQ") = bl.Cells(i, "AA") ws.Cells(i, "B") = bl.Cells(i, "AB") ws.Cells(i, "A") = bl.Cells(i, "AD") ws.Cells(i, "BW") = bl.Cells(i, "AK") ws.Cells(i, "BH") = bl.Cells(i, "AL") ws.Cells(i, "BR") = bl.Cells(i, "AM") ws.Cells(i, "AL") = bl.Cells(i, "AP") ws.Cells(i, "AP") = bl.Cells(i, "BA") ws.Cells(i, "AQ") = bl.Cells(i, "BB") ws.Cells(i, "AU") = bl.Cells(i, "BC") ws.Cells(i, "AO") = bl.Cells(i, "BK") ws.Cells(i, "AT") = bl.Cells(i, "BO") Next i 

复制整个列而不是单元格!

 ws.Range("AG2:AH" & lastrow) = bl.Range("F2:G" & lastrow) '2 cols at once here ws.Range("AB2:AB" & lastrow) = bl.Range("N2:N" & lastrow) '.Value is implicit 

(等等..)应该快1000倍。

在VBA和Excel之间交换数据对于CPU来说是非常昂贵的。 因此转移大块更有效率。

你试一试:

 ws.Range("AG2:AG" & lastrow).Value = bl.Range("F2:F" & lastrow).Value ws.Range("AH2:AH" & lastrow).Value = bl.Range("G2:G" & lastrow).Value ws.Range("AB2:AB" & lastrow).Value = bl.Range("N2:N" & lastrow).Value 

等等其他的列

除了上面提供的答案为我工作,我张贴了另一个类似的问题,从另一个不同的用户的答案,我想我应该分享在这里受益于其他人环顾四周。

https://stackoverflow.com/a/41951959/7483682