views:

83

answers:

3

Right now I have a macro PopulateYearlyValues But it seems to me it's taking way 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:H44 that needs to be populated with the SUM of range AA7:AX44 but.. it's only 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 the macro I have is taking way too long.. Is there any other way to do this?

+1  A: 

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

marg
Thank you so much. That link also helped me by removing another loop in a different sub. Didn't know you could do .Range(.Cells(7, 2), .Cells(44, 3)).Value = .Range(.Cells(7, m), .Cells(44, (m + 1)).ValueI'm a PHP developer and was asked to make a few macros in Excel. Never done this before.
N. Lucas
+1  A: 

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.

Michael
A: 

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
N. Lucas