Speed up an Excel Macro?

后端 未结 4 1115
挽巷
挽巷 2020-12-21 08:43

Right now I have a macro PopulateYearlyValues But it seems to me it\'s taking way too long

Sub PopulateYearlyValues(ByVal Month As Range)
    Di         


        
相关标签:
4条回答
  • 2020-12-21 08:59

    Can I suggest a formula: SUMIF will sum base on a criteria

    SUMIF($AA$99:$AX$99;"=1";$AA7:$AX7) ' for G column
    SUMIF($AA$99:$AX$99;"=2";$AA7:$AX7) ' for H column
    

    The cells in Row 99 (can be hidden) have the condition based on Month.Value and the column, yielding "0"(don`t sum yet), "1"(sum to G column) or "2" (sum to H column):

    =IF(Month.Value>=ROUNDDOWN((COLUMN(AA3)-25)/2;0);2-MOD(COLUMN(AA3);2);0)
    

    Regards

    Alen

    0 讨论(0)
  • 2020-12-21 09:05

    I can't tell if this is something I changed when I updated my PopulateYearlyValues or what but now I'm running into another problem..

    G7 = Actual | H7 = Plan | I7 = (G7 - H7) | J7 =IF(OR(G7 = 0, I7 = 0), 0, I7 / G7)

    Since I changed the above sub, I7 and J7 stay the same when the month selection is changed.. G7 and H7 are updating properly though.

    I found a fix to it, but it seems there should be a better way..

    For i = 7 To 44
        For j = 1 To Application.WorksheetFunction.Match(UCase(Target.Value), .Range("AA5:AX5"), 0)
            .Range("I" & i).Value = "=G" & i & "-H" & i &")"
            .Range("J" & i).Value = "=IF(OR(G" & i & "=0, H" & i & "=0), 0, I" & i & " / G" & i & ")"
        Next j
    Next i
    
    0 讨论(0)
  • 2020-12-21 09:06

    As a follow up, you can also set Application.EnableEvents to false (and subsequently return it to true) to potentially save time depending on what your procedure does.

    0 讨论(0)
  • 2020-12-21 09:11

    You can try the usual vba optimization methods of setting calculation to manual and disabling ScreenUpdating .

    Dim calc As XlCalculation
    calc = Application.Calculation
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Application.ScreenUpdating = True
    Application.Calculation = calc
    

    Put your code or function call between Application.Calculation = xlCalculationManual and Application.ScreenUpdating = True

    This is from my previous Post

    0 讨论(0)
提交回复
热议问题