【问题标题】:VBA: Quickest way to copy and paste rows of dataVBA:复制和粘贴数据行的最快方法
【发布时间】:2017-06-14 17:00:11
【问题描述】:

我想知道是否有更快的方法将数据从工作表的一列复制并粘贴到工作表的另一列。这是我所拥有的示例代码,它运行缓慢,因为它为我正在复制的每一列复制超过 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

【问题讨论】:

  • 尝试提供反馈以回答您在 SO 上发布的问题。您还没有接受任何答案,这将影响您以后寻求帮助。

标签: vba excel


【解决方案1】:

复制整列而不是单元格!

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 来说非常昂贵。因此,传输大块更有效。

【讨论】:

    【解决方案2】:

    试试吧:

    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
    

    其余列以此类推

    【讨论】:

      【解决方案3】:

      除了上面提供的对我有用的答案之外,我还发布了另一个类似的问题,其中有来自不同用户的另一个答案,我认为我应该在这里分享,以使其他人在 SO 中四处张望。

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

      【讨论】:

        猜你喜欢
        • 2013-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多