如何将基本循环转换为内存数组?

时间:2014-08-21 19:27:21

标签: arrays excel-vba vba excel

我有一些可行的代码,但我试图通过使用内存数组来提高其性能。这是有效的代码。

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual



    Dim wsBalance As Worksheet
    Dim i As Integer

    Set wsBalance = ThisWorkbook.Sheets("Balance")

    With wsBalance

        For i = 4 To 10

           .Range("I3").Value = .Range("J" & i).Value

           wsBalance.Calculate

           .Range("K" & i).Value = .Range("H14982") * -1


        Next i

    End With



Application.ScreenUpdating = True
Applicaiton.Calculation = xlCalculationAutomatic

我尝试使用内存数组让这段代码更快,但我做错了。这是我的代码:

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

    Dim wsBalance As Worksheet
    Dim var1 As Variant, var2 As Variant, var3 As Variant, var4 As Variant
    Dim i As Integer


    Set wsBalance = ThisWorkbook.Sheets("Balance")


    With wsBalance

        Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row

        var1 = .Range("I3")
        var2 = .Range("J4:J" & Lastrow)
        var3 = .Range("K4:K" & Lastrow)
        var4 = .Range("H14982") * -1

        For i = LBound(var2, 1) To UBound(var2, 1)

            var1 = var2
            var3 = var4

        Next i

        .Range("K4:K" & Lastrow).Value = var4

    End With




Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

当我运行代码时,我获得了粘贴到K列的相同值。有什么建议吗?

谢谢!

0 个答案:

没有答案