• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Extremely Slow Running Macro Code

Dokat

Member
Hi,

I have a simple SUMIFS VBA code that i use rather than a sumifs function. Code works however it runs extremely slow. Anyone has suggestions to optimize and speed up run time?

Code:
Sub SUMIFSLWFORMBRAND()

    Const TOTALSROW = 61
    Dim i, x, y, z, t, f As Long
  
    With Sheets("HDD")
  
        .Cells(TOTALSROW, 7) = WorksheetFunction.SumIf(Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW, 5), Sheets("Source").Range("av:av"))

        .Cells(TOTALSROW, 8) = WorksheetFunction.SumIf(Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW, 5), Sheets("Source").Range("aw:aw"))
              
        For x = 2 To 22
              
        .Cells(TOTALSROW + x, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + x, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + x, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + x, 6))

        .Cells(TOTALSROW + x, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + x, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + x, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + x, 6))
      
        For i = 1 To 1
      
        .Cells(TOTALSROW + i, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + i, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + i, 4))

        .Cells(TOTALSROW + i, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + i, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + i, 4))
      
        For y = 24 To 24
      
        .Cells(TOTALSROW + y, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + y, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + y, 4))

        .Cells(TOTALSROW + y, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + y, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + y, 4))

        For z = 25 To 37
              
        .Cells(TOTALSROW + z, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + z, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + z, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + z, 6))

        .Cells(TOTALSROW + z, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + z, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + z, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + z, 6))
    
      For t = 38 To 38
      
        .Cells(TOTALSROW + t, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + t, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + t, 4))

        .Cells(TOTALSROW + t, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + t, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + t, 4))
  
     For f = 39 To 48
              
        .Cells(TOTALSROW + f, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + f, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + f, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + f, 6))

        .Cells(TOTALSROW + f, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + f, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + f, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + f, 6))
    
                        Next f
                    Next t
                Next z
             Next y
           Next i
        Next x
    End With
End Sub
 
There are several issues here

1. Const TOTALSROW = 61
this dimensions TOTALSROW as a variant
I think it should be
Const TOTALSROW as Integer = 61

2. Dim i, x, y, z, t, f As Long
is actual the same as
Dim i as variant, x as variant, y as variant, z as variant, t as variant, f as Long

where I think you actually meant
Dim i as Long, x as Long, y as Long, z as Long, t as long, f as Long

3. Because i, x,y,z,t & f are all small numbers you should use
Dim i as Integer, x as Integer, y as Integer, z as Integer, t as Integer, f as Integer

4. Your choice of loops in side loops is confusing as none of the initial variables are used in inside loops and hence will be redundant

I think it should be laid out as
Code:
Const TOTALSROW = 61
Dim i As Integer, x As Integer, y As Integer, z As Integer, t As Integer, f As Integer
  
  With Sheets("HDD")
  .Cells(TOTALSROW, 7) = WorksheetFunction.SumIf(Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW, 5), Sheets("Source").Range("av:av"))
  .Cells(TOTALSROW, 8) = WorksheetFunction.SumIf(Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW, 5), Sheets("Source").Range("aw:aw"))
  
  i = 1
  .Cells(TOTALSROW + i, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + i, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + i, 4))
  .Cells(TOTALSROW + i, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + i, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + i, 4))
  
  For x = 2 To 22
  .Cells(TOTALSROW + x, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + x, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + x, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + x, 6))
  .Cells(TOTALSROW + x, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + x, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + x, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + x, 6))
  Next x
  
  y = 24
  .Cells(TOTALSROW + y, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + y, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + y, 4))
  .Cells(TOTALSROW + y, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + y, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + y, 4))
  
  For z = 25 To 37
  .Cells(TOTALSROW + z, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + z, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + z, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + z, 6))
  .Cells(TOTALSROW + z, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + z, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + z, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + z, 6))
  Next z
  
  t = 38
  .Cells(TOTALSROW + t, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + t, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + t, 4))
  .Cells(TOTALSROW + t, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + t, 5), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + t, 4))
  
  For f = 39 To 48
  .Cells(TOTALSROW + f, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + f, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + f, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + f, 6))
  .Cells(TOTALSROW + f, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("CC:CC"), .Cells(TOTALSROW + f, 4), Sheets("Source").Range("cb:cb"), .Cells(TOTALSROW + f, 5), Sheets("Source").Range("CG:CG"), .Cells(TOTALSROW + f, 6))
  Next f
  
  End With

Note that i, y & t don't need loops as they are single fixed values
Also note that I changed the order of the blocks to keep them in order from 1 to 48

5. The Sumifs are using the whole Columns

I am going to take a guess that the table on the Source worksheet has lets say 20,000 rows
But your Sumifs are checking teh whole 1,000,000 rows
that is 50 times more data than it needs to and you are doing it many many times

I wuld work out what the last row is and then change that formula to use row 1 to Row x, whatever x is

Code:
Sub SUMIFSLWFORMBRAND()

Const TOTALSROW = 61
Dim i As Integer, x As Integer, y As Integer, z As Integer, t As Integer, f As Integer
Dim lr As Long
lr = Worksheets("Source").Range("A" & Rows.Count).End(xlUp).Row
  
  With Sheets("HDD")
  
  .Cells(TOTALSROW, 7) = WorksheetFunction.SumIf(Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW, 5), Sheets("Source").Range("av1:av" & CStr(lr)))
  .Cells(TOTALSROW, 8) = WorksheetFunction.SumIf(Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW, 5), Sheets("Source").Range("av1:av" & CStr(lr)))
  
  i = 1
  .Cells(TOTALSROW + i, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av1:av" & CStr(lr)), Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW + i, 5), Sheets("Source").Range("Cc1:cc" & CStr(lr)), .Cells(TOTALSROW + i, 4))
  .Cells(TOTALSROW + i, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw1:aw" & CStr(lr)), Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW + i, 5), Sheets("Source").Range("Cc1:cc" & CStr(lr)), .Cells(TOTALSROW + i, 4))
  
  For x = 2 To 22
  .Cells(TOTALSROW + x, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av1:av" & CStr(lr)), Sheets("Source").Range("Cc1:cc" & CStr(lr)), .Cells(TOTALSROW + x, 4), Sheets("Source").Range("Cb1:cb" & CStr(lr)), .Cells(TOTALSROW + x, 5), Sheets("Source").Range("Cg1:cg" & CStr(lr)), .Cells(TOTALSROW + x, 6))
  .Cells(TOTALSROW + x, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw1:aw" & CStr(lr)), Sheets("Source").Range("Cc1:cc" & CStr(lr)), .Cells(TOTALSROW + x, 4), Sheets("Source").Range("Cb1:cb" & CStr(lr)), .Cells(TOTALSROW + x, 5), Sheets("Source").Range("Cg1:cg" & CStr(lr)), .Cells(TOTALSROW + x, 6))
  Next x
  
  y = 24
  .Cells(TOTALSROW + y, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av1:av" & CStr(lr)), Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW + y, 5), Sheets("Source").Range("Cc1:cc" & CStr(lr)), .Cells(TOTALSROW + y, 4))
  .Cells(TOTALSROW + y, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw1:aw" & CStr(lr)), Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW + y, 5), Sheets("Source").Range("Cc1:cc" & CStr(lr)), .Cells(TOTALSROW + y, 4))
  
  For z = 25 To 37
  .Cells(TOTALSROW + z, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av1:av" & CStr(lr)), Sheets("Source").Range("cc1:cc" & CStr(lr)), .Cells(TOTALSROW + z, 4), Sheets("Source").Range("Cb1:cb" & CStr(lr)), .Cells(TOTALSROW + z, 5), Sheets("Source").Range("Cg1:cg" & CStr(lr)), .Cells(TOTALSROW + z, 6))
  .Cells(TOTALSROW + z, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw1:aw" & CStr(lr)), Sheets("Source").Range("cc1:cc" & CStr(lr)), .Cells(TOTALSROW + z, 4), Sheets("Source").Range("Cb1:cb" & CStr(lr)), .Cells(TOTALSROW + z, 5), Sheets("Source").Range("Cg1:cg" & CStr(lr)), .Cells(TOTALSROW + z, 6))
  Next z
  
  t = 38
  .Cells(TOTALSROW + t, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av1:av" & CStr(lr)), Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW + t, 5), Sheets("Source").Range("Cc1:cc" & CStr(lr)), .Cells(TOTALSROW + t, 4))
  .Cells(TOTALSROW + t, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw1:aw" & CStr(lr)), Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW + t, 5), Sheets("Source").Range("Cc1:cc" & CStr(lr)), .Cells(TOTALSROW + t, 4))
  
  For f = 39 To 48
  .Cells(TOTALSROW + f, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("cc1:cc" & CStr(lr)), .Cells(TOTALSROW + f, 4), Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW + f, 5), Sheets("Source").Range("Cg1:cg" & CStr(lr)), .Cells(TOTALSROW + f, 6))
  .Cells(TOTALSROW + f, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw:aw"), Sheets("Source").Range("cc1:cc" & CStr(lr)), .Cells(TOTALSROW + f, 4), Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW + f, 5), Sheets("Source").Range("Cg1:cg" & CStr(lr)), .Cells(TOTALSROW + f, 6))
  Next f
  
  End With
End Sub

Finally without a file i cannot check if the above works

Please let us know
 
Hi Thanks for the detailed explanation. It partly worked but end up giving error message Run time Error 1004 Unable to get sumifs property of the worksheet function class. Not sure what caused it.

Below code line was highlighted in yellow

Code:
  .Cells(TOTALSROW + f, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av:av"), Sheets("Source").Range("cc1:cc" & CStr(lr)), .Cells(TOTALSROW + f, 4), Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW + f, 5), Sheets("Source").Range("Cg1:cg" & CStr(lr)), .Cells(TOTALSROW + f, 6))

Thanks
 
Obviously I missed extending one of the ranges

that line and the next line should be

Code:
.Cells(TOTALSROW + f, 7) = WorksheetFunction.SumIfs(Sheets("Source").Range("av1:av" & CStr(lr)), Sheets("Source").Range("cc1:cc" & CStr(lr)), .Cells(TOTALSROW + f, 4), Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW + f, 5), Sheets("Source").Range("Cg1:cg" & CStr(lr)), .Cells(TOTALSROW + f, 6))
.Cells(TOTALSROW + f, 8) = WorksheetFunction.SumIfs(Sheets("Source").Range("aw1:aw" & CStr(lr)), Sheets("Source").Range("cc1:cc" & CStr(lr)), .Cells(TOTALSROW + f, 4), Sheets("Source").Range("cb1:cb" & CStr(lr)), .Cells(TOTALSROW + f, 5), Sheets("Source").Range("Cg1:cg" & CStr(lr)), .Cells(TOTALSROW + f, 6))
  Next f
 
I just realized after updating the new code i am coming across issue with the below one which uses results from the above code. Its a simple subtract, divide, % formulas. This code use to run but after changing the prior code it stopped. Do you know what may cause this

Code:
Sub MathLWFORMBRAND()

    Dim i, x, z As Long
    Dim condition As Range
   
    Application.ScreenUpdating = False

    With Sheets("FE")
   
       
        For i = 73 To 95
       
            .Cells(i, 9) = .Cells(i, 7) - .Cells(i, 8)

            If .Cells(i, 8) <> 0 Then
                .Cells(i, 10) = .Cells(i, 7) / .Cells(i, 8) - 1
            End If
         
            If .Range("G74") <> 0 Then
                .Cells(i, 11) = .Cells(i, 7) / .Range("G74") * 100
            End If
       
            If .Range("H74") <> 0 Then
                .Cells(i, 12) = .Cells(i, 8) / .Range("H74") * 100
            End If
       
            .Cells(i, 13) = .Cells(i, 11) - .Cells(i, 12)
       
        For x = 97 To 110
         
               .Cells(x, 9) = .Cells(x, 7) - .Cells(x, 8)

            If .Cells(x, 8) <> 0 Then
                .Cells(x, 10) = .Cells(x, 7) / .Cells(x, 8) - 1
            End If
         
            If .Range("G97") <> 0 Then
                .Cells(x, 11) = .Cells(x, 7) / .Range("G97") * 100
            End If
       
            If .Range("H97") <> 0 Then
                .Cells(x, 12) = .Cells(x, 8) / .Range("H97") * 100
            End If
       
            .Cells(x, 13) = .Cells(x, 11) - .Cells(x, 12)
           
           
        For z = 111 To 124
         
               .Cells(z, 9) = .Cells(z, 7) - .Cells(z, 8)

            If .Cells(z, 8) <> 0 Then
                .Cells(z, 10) = .Cells(z, 7) / .Cells(z, 8) - 1
            End If
         
            If .Range("G111") <> 0 Then
                .Cells(z, 11) = .Cells(z, 7) / .Range("G111") * 100
            End If
       
            If .Range("H111") <> 0 Then
                .Cells(z, 12) = .Cells(z, 8) / .Range("H111") * 100
            End If
       
            .Cells(z, 13) = .Cells(z, 11) - .Cells(z, 12)
           
                Next z
            Next x
        Next i
    End With
   
    Application.ScreenUpdating = True
End Sub
 
Without a file I have no idea

Did my revised code work?
We’re the results the same as your old slow code ?

ps it’s looking at a totally different worksheet so I don’t see the relationship ?
 
I just realized i sent the wrong code. Below is the correct one.

Code:
Sub MathLWFORMBRAND()

    Dim i, x, z As Long
    Dim condition As Range
   
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

    With Sheets("HDD")
   
        For i = 61 To 84
       
            .Cells(i, 9) = .Cells(i, 7) - .Cells(i, 8)

            If .Cells(i, 8) <> 0 Then
                .Cells(i, 10) = .Cells(i, 7) / .Cells(i, 8) - 1
            End If
         
            If .Range("G61") <> 0 Then
                .Cells(i, 11) = .Cells(i, 7) / .Range("G61") * 100
            End If
       
            If .Range("H61") <> 0 Then
                .Cells(i, 12) = .Cells(i, 8) / .Range("H61") * 100
            End If
       
            .Cells(i, 13) = .Cells(i, 11) - .Cells(i, 12)
       
        For x = 85 To 98
         
               .Cells(x, 9) = .Cells(x, 7) - .Cells(x, 8)

            If .Cells(x, 8) <> 0 Then
                .Cells(x, 10) = .Cells(x, 7) / .Cells(x, 8) - 1
            End If
         
            If .Range("G85") <> 0 Then
                .Cells(x, 11) = .Cells(x, 7) / .Range("G85") * 100
            End If
       
            If .Range("H85") <> 0 Then
                .Cells(x, 12) = .Cells(x, 8) / .Range("H85") * 100
            End If
       
            .Cells(x, 13) = .Cells(x, 11) - .Cells(x, 12)
           
           
        For z = 99 To 108
         
               .Cells(z, 9) = .Cells(z, 7) - .Cells(z, 8)

            If .Cells(z, 8) <> 0 Then
                .Cells(z, 10) = .Cells(z, 7) / .Cells(z, 8) - 1
            End If
         
            If .Range("G99") <> 0 Then
                .Cells(z, 11) = .Cells(z, 7) / .Range("G99") * 100
            End If
       
            If .Range("H99") <> 0 Then
                .Cells(z, 12) = .Cells(z, 8) / .Range("H99") * 100
            End If
       
            .Cells(z, 13) = .Cells(z, 11) - .Cells(z, 12)
           
                Next z
            Next x
        Next i
    End With
   Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
is the second macro ok now?

Does the original code run faster?
 
Last edited:
Back
Top