Compound If Statement and VBA

jswhip

Board Regular
Joined
Jan 19, 2005
Messages
69
Within a macro, how does one check for division by zero? I know there are instances where I will get a division by zero error, thus I want to check for that error proceeding. I know how to perform the check if I have am using a formula within a cell, but don't the syntax for using it within a formula.

For example, in the following code, I can't put a check for a zero divisor as a condition for the division operation (eg - "Cells(x, 7) / Cells(x, 6)").


If Cells(x, 23) > 5000 Or Cells(x, 24) > 0 Then
Cells(x, FinalCol + 1) = "IntPrin"
ElseIf Cells(x, 7) / Cells(x, 6) < 0.8 Or _
Cells(x, 11) - Cells(x, 10) > 0 Or _
(Cells(x, 33) = "Y" Or Cells(x, 34) = "Y" Or _
Cells(x, 35) = "Y" Or Cells(x, 36) = "Y" Or _
Cells(x, 37) = "Y" Or Cells(x, 38) = "Y" Or _
Cells(x, 39) = "Y" Or Cells(x, 40) = "Y") Then
Cells(x, FinalCol + 1) = "ProbSubCon"

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
Try using something like this

Code:
ElseIf Not IsError(Cells(x, 7) / Cells(x, 6)) AND Cells(x, 7) / Cells(x, 6)) < 0.8
 
Upvote 0
You could rewrite it to avoid the division:

Code:
    If Cells(x, 23) > 5000 Or _
       Cells(x, 24) > 0 Then
        Cells(x, FinalCol + 1) = "IntPrin"
    ElseIf Cells(x, 7) < Cells(x, 6) * 0.8 Or _
           Cells(x, 11) > Cells(x, 10) Or _
           Cells(x, 33) = "Y" Or _
           Cells(x, 34) = "Y" Or _
           Cells(x, 35) = "Y" Or _
           Cells(x, 36) = "Y" Or _
           Cells(x, 37) = "Y" Or _
           Cells(x, 38) = "Y" Or _
           Cells(x, 39) = "Y" Or _
           Cells(x, 40) = "Y" Then
        Cells(x, FinalCol + 1) = "ProbSubCon"
    End If
Or, more briefly,
Code:
    If Cells(x, 23) > 5000 Or _
       Cells(x, 24) > 0 Then
        Cells(x, FinalCol + 1) = "IntPrin"
    ElseIf Cells(x, 7) < Cells(x, 6) * 0.8 Or _
           Cells(x, 11) > Cells(x, 10) Or _
           WorksheetFunction.CountIf(Cells(x, 33).Resize(, 8), "Y") > 0 Then
        Cells(x, FinalCol + 1) = "ProbSubCon"
    End If
 
Upvote 0
Very good replies all around.

I had also used "On Error Resume Next", but didn't want to inadvertently miss an error.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,337
Members
452,907
Latest member
Roland Deschain

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