Error Handling in Macro

tlc_in_OK

Board Regular
Joined
Jun 27, 2011
Messages
56
I have a macro that makes several calculations, but I can't figure out how to handle a divide by zero error ("overflow" for the macro). I've tried using the IfError function and get the same result. Ideally, I'd like it to place a "0" in the cell if there are no results for the denominator. Can someone help? Here's the calculation line. . .

Code:
    .Range("I39").Value = WorksheetFunction.SumIf(Worksheets("All_Properties").Range("H:H"), "Rejected", _
            Worksheets("All_Properties").Range("K:K")) / WorksheetFunction.SumIf(Worksheets("All_Properties").Range("H:H"), _
            "Rejected", Worksheets("All_Properties").Range("L:L"))
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
What I tend to do is to put the denominator and numerator into two variables.

Code:
dim nNumberator as Double, nDenominator as Double
 
nNumerator = WorksheetFunction.SumIf(Worksheets("All_Properties").Range("H:H"), "Rejected", _
            Worksheets("All_Properties").Range("K:K"))

nDeonominator = WorksheetFunction.SumIf(Worksheets("All_Properties").Range("H:H"), _
            "Rejected", Worksheets("All_Properties").Range("L:L"))
 
if nDenominator = 0 then
  .range("I39").Value = 0
else
  .range ("I39") = nNumerator / nDenominator
end if
 
Upvote 0
Thanks, that will work. . .guess I was just trying to do too much without using variables. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,216,477
Messages
6,130,879
Members
449,603
Latest member
dizze90

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