Refactor VBA to make it work faster

ouvay

Board Regular
Joined
Jun 9, 2022
Messages
131
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I wrote this bit of code, but it takes a while to calculate considering how many times I use it - I believe it takes really long because it runs the code through the entire table column.

I am looking to try and get this to work faster, maybe by calculating only when there is a change or perhaps another way I am unaware of.

Please have a look at let me know how I may go about to doing this.

the code gives me the desired output.. It just takes its sweet time in doing so.

VBA Code:
Sub checkProgress()
Application.ScreenUpdating = False

Dim i As Long

For i = 2 To Rows.Count

    If Range("C" & i).Value = "" Or Range("I" & i).Value <> "" Or Range("A" & i).Value <> "" Then
    Else
        Range("I" & i).Value = Application.WorksheetFunction.SumIfs(Range("G2:G" & i), Range("C2:C" & i), Range("C" & i), Range("D2:D" & i), Range("D" & i))
        Range("H" & i).Value = Range("E" & i).Value - Range("I" & i).Value
    End If
Next i

Application.ScreenUpdating = True
End Sub

Posting image for visual idea of what the code should do

enter image description here
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
See if the suggestions here make a difference.

 
Upvote 0
See if the suggestions here make a difference.

Thanks for suggesting that

I already have
Application.ScreenUpdating = False
in my code .. I'll go ahead and implement the rest of those too, but I doubt it would make a noticeable change.

My past research had shown me that turning ScreenUpdating off puts a real dent in processing time - the rest, not so much.. but doesn't hurt to try I suppose! ty
 
Upvote 0
Welcome to the MrExcel board!

Yes, you are processing over a million rows when you are probably only using a very small (relatively) number.
It looks like that may well be a formal Excel table (ListObject). If that is so, give this a try with a copy of your workbook.
Assuming the table is the only table on the worksheet, or if more then this is ListObjects(1)

VBA Code:
Sub checkProgress_v2()
  Dim i As Long
 
  Application.ScreenUpdating = False
  With ActiveSheet.ListObjects(1).DataBodyRange
    For i = 1 To .Rows.Count
        If .Range("C" & i).Value <> "" And .Range("I" & i).Value = "" And .Range("A" & i).Value = "" Then
            .Range("I" & i).Value = Application.WorksheetFunction.SumIfs(.Range("G1:G" & i), .Range("C1:C" & i), .Range("C" & i), .Range("D1:D" & i), .Range("D" & i))
            .Range("H" & i).Value = .Range("E" & i).Value - .Range("I" & i).Value
        End If
    Next i
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Welcome to the MrExcel board!

Yes, you are processing over a million rows when you are probably only using a very small (relatively) number.
It looks like that may well be a formal Excel table (ListObject). If that is so, give this a try with a copy of your workbook.
Assuming the table is the only table on the worksheet, or if more then this is ListObjects(1)

VBA Code:
Sub checkProgress_v2()
  Dim i As Long
 
  Application.ScreenUpdating = False
  With ActiveSheet.ListObjects(1).DataBodyRange
    For i = 1 To .Rows.Count
        If .Range("C" & i).Value <> "" And .Range("I" & i).Value = "" And .Range("A" & i).Value = "" Then
            .Range("I" & i).Value = Application.WorksheetFunction.SumIfs(.Range("G1:G" & i), .Range("C1:C" & i), .Range("C" & i), .Range("D1:D" & i), .Range("D" & i))
            .Range("H" & i).Value = .Range("E" & i).Value - .Range("I" & i).Value
        End If
    Next i
  End With
  Application.ScreenUpdating = True
End Sub
Good grief that was insanely fast!!!

Thank you sooo much!!
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,643
Members
449,093
Latest member
Ahmad123098

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top