Help with avoiding division by zero error in VBA

carstenp

Board Regular
Joined
May 27, 2010
Messages
148
I've written this code in an attempt to put a null value in the cell if the formula returns the division by zero error, but I get the 'Overflow' message when I hit the 'If' line. Any ideas why that's happening?
Code:
                                    If IsError(.Cells(SummaryRow2 + t, m) / .Cells(SummaryRow1 + t, m)) Then
                                        .Cells(SummaryRow3 + t, m).Value = Null
                                    Else
                                        .Cells(SummaryRow3 + t, m).Formula = .Cells(SummaryRow2 + t, m) / .Cells(SummaryRow1 + t, m)
                                    End If
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
That's not it. I tried changing them all to long and it still gives the same error message. I even tried changing the code to this and defining p as a double
Code:
                                    On Error GoTo ErrorHandler:
                                        p = .Cells(SummaryRow2 + t, m) / .Cells(SummaryRow1 + t, m)
                                        .Cells(SummaryRow3 + t, m) = p
                                    
ErrorHandler:
    If Err.Number = 11 Then
        .Cells(SummaryRow3 + t, m).Value = Null
        Resume AfterError:
    End If
AfterError:
Any help would be greatly appreciated
 
Upvote 0
If you have 0 / 0 you get overflow error, if you have x<>0 / 0 you get division by zero error. I think that the simpliest and cleanest way is just like this:
Code:
If .Cells(summaryrow1 + t, m) = 0 Then ' if denominator equals 0 then division by 0 occurs
  .Cells(summaryrow3 + t, m).Value = Null
Else
  .Cells(summaryrow3 + t, m).Formula = .Cells(summaryrow2 + t, m) / .Cells(summaryrow1 + t, m)
End If
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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