加快我的VBA代码

时间:2014-12-15 15:10:35

标签: excel performance vba excel-vba

我有一个代码,它在8列和几百行中进行一些计算。代码目前有效,但需要一段时间才能运行。我想加快我的代码速度。我听说过使用变量数组可以加快宏的速度。目前,我已经设法使用数组读取数据并将其存储在数组中,然后将其输出到单独的工作表。以下代码中未声明的变量是公开的。

Sub BBHCashRecon()
            With Application
                .ScreenUpdating = False
                .DisplayStatusBar = False
                .Calculation = xlCalculationManual
                .EnableEvents = False
            End With


'Worksheets
     Set BBHNAV = Worksheets("BBH NAV")

   With BBHNAV
LastRow = .Cells(.Rows.Count, "J").End(xlUp).Row
'For each row in the 'BBH NAV' sheet with data...
    For n = 2 To LastRow
        'Unique Identifier
        .Range("A" & n).Value = .Range("J" & n).Value & .Range("M" & n).Value
    Next n

    For n = 2 To LastRow

        'Base Market Value
            .Range("B2").FormulaArray = "=IFERROR(INDEX('T-1 DATA'!G:G,MATCH(1,('T-1 DATA'!A:A='BBH NAV'!A2)*('T-1 DATA'!D:D='BBH NAV'!L2),0),1)," & """NO DATA""" & ")"
            .Range("B2:B" & LastRow).FillDown
        'calculate columns B otherwise column C for some reason is using the value in B2 which is filldown when running the calculation
        .Range("B" & n).Calculate

        '% change in NAV
        If .Range("B" & n).Value = "NO DATA" Then
            .Range("C" & n).Value = "NO DATA"
        Else
            .Range("C" & n).Value = WorksheetFunction.IfError((.Range("O" & n).Value / .Range("B" & n).Value) - 1, "ERROR")
        End If

        'change in NAV
        If .Range("B" & n).Value = "NO DATA" Then
            .Range("D" & n).Value = "NO DATA"
        Else
            .Range("D" & n).Value = WorksheetFunction.IfError(.Range("O" & n).Value - .Range("B" & n).Value, "ERROR")
        End If

        'Market Value
        .Range("E" & n).Value = WorksheetFunction.SumIf(.Range("J:J"), .Range("J" & n).Value, .Range("O:O"))

        'Column F and G
            'ID for assets
                ID = Left(.Range("L" & n).Value, 4)
                If ID = "CASH" Then
                    .Range("F" & n).Value = 1
                Else
                    .Range("F" & n).Value = 0
                End If

        'BBH# & column F
        .Range("G" & n).Value = .Range("J" & n).Value & .Range("F" & n).Value

        'Cash Balance
        .Range("H" & n).Value = WorksheetFunction.SumIf(.Range("G:G"), .Range("G" & n).Value, .Range("O:O")) * .Range("F" & n).Value

    Next n

       'conditional formatting for column c of values not between plus or minus 3%
            With .Range("C2:C" & LastRow)
                .Style = "Percent": .NumberFormat = "0.00%"
                .FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, Formula1:="=-0.03", Formula2:="=0.03": .FormatConditions(.FormatConditions.Count).SetFirstPriority
            End With
            With .Range("C2:C" & LastRow).FormatConditions(1).Interior
                .PatternColorIndex = xlAutomatic: .Color = 14481404
            End With
            .Range("C2:C" & LastRow).FormatConditions(1).StopIfTrue = False
 End With
      With Application
       .ScreenUpdating = True
       .DisplayStatusBar = True
       .Calculation = xlCalculationAutomatic
       .EnableEvents = True
   End With
   End Sub

1 个答案:

答案 0 :(得分:0)

这是让你入门的东西:

Excel非常智能,可以增加行引用。

此:

LastRow = .Cells(.Rows.Count, "J").End(xlUp).Row
'For each row in the 'BBH NAV' sheet with data...
    For n = 2 To LastRow
        'Unique Identifier
        .Range("A" & n).Value = .Range("J" & n).Value & .Range("M" & n).Value
    Next n

可以这样写,完全避免循环:

Range("N2:N" & Range("J" & Rows.Count).End(xlUp).Row).Formula = "=J2 & M2"
Range("N2:N" & Range("J" & Rows.Count).End(xlUp).Row).copy
Range("N2:N" & Range("J" & Rows.Count).End(xlUp).Row).pastespecial xlPasteValues
application.CutCopyMode = false

它插入公式,复制它,粘贴为自身的值然后将cutcopymode设置为false,如果要复制并粘贴其他内容,则不需要此最后一行,因为下一个副本会杀死此副本引用

尝试更换当前循环以使用此方法,如果卡住则回发。您的if语句也可以转换为一个简单的if公式,该公式将在工作表中使用,然后使用我发布的相同方法完成。