Speed ​​up Excel macro?

I have a macro right now PopulateYearlyValuesBut it seems to me that it is too long

Sub PopulateYearlyValues(ByVal Month As Range)
    Dim c As Double
    Dim s As Double
    c = Application.WorksheetFunction.Match(UCase(Month.Value), ActiveSheet.Range("AA5:AX5"), 0)
    s = (ActiveSheet.Range("AA5").Column - 1)
    With ActiveSheet
        Dim i As Integer
        Dim j As Integer
        For i = 7 To 44
            .Range("G" & i).Value = 0
            .Range("H" & i).Value = 0
            For j = 1 To c
                .Range("G" & i).Value = (.Range("G" & i).Value + .Cells(i, s).Offset(0, j))
                .Range("H" & i).Value = (.Range("H" & i).Value + .Cells(i, s).Offset(0, (j + 1)))
                j = j + 1
            Next j
        Next i
    End With
End Sub

I have a range G7:H44that needs to be filled with a SUMrange AA7:AX44, but .. this is just every other column:

If Month.Value = "January"
    G7  = SUM(AA7)
    H7  = SUM(AB7)
    ...
    G44 = SUM(AA44)
    H44 = SUM(AB44)
End If

If Month.Value = "April"
    G7  = SUM(AA7, AC7, AE7, AG7)
    H7  = SUM(AB7, AD7, AF7, AH7)
    ...
    G44 = SUM(AA44, AC44, AE44, AG44)
    H44 = SUM(AB44, AD44, AF44, AH44)
End If

But my macro is too long. Is there any other way to do this?

+3
source share
4 answers

You can try the usual vba optimization methods to manually configure the calculation and turn off 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 = xlCalculationManualandApplication.ScreenUpdating = True

This is from my previous post

+4
source

Application.EnableEvents false ( true), , .

+1

, , PopulateYearlyValues -, .

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

, I7 J7 , . G7 H7 , .

, , , .

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

: SUMIF

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 a condition based on Month.Value and a column giving “0” (not yet charged), “1” (amount to column G), or “2” (amount in column H):

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

Hi

Alain

0
source

Source: https://habr.com/ru/post/1539497/


All Articles