# vba iferror

#### SQUIDD

##### Well-known Member
Hi

I have a line of code, but sometimes when i have zeros, it gives me a "divide by zero error"
so, when i do this as a formula i use iferror(formula,"")

so my question is, can i wrap this code in an iferror sttatment in vba

Code:
``result = 100 / Application.CountIf(Sheets("TRAP" & a - 1).Range("C:C"), a - 1) * Application.WorksheetFunction.CountIfs(Sheets("TRAP" & a - 1).Range("C:C"), a - 1, Sheets("TRAP" & a - 1).Range("E:E"), 1)``

i did try application.iferror
dave

### Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
result = WorksheetFunction.IfError( _
100 / WorksheetFunction.CountIf(Sheets("TRAP" & a - 1).Range("C:C"), a - 1) * _
WorksheetFunction.CountIfs(Sheets("TRAP" & a - 1).Range("C:C"), a - 1, Sheets("TRAP" & a - 1).Range("E:E"), 1), _
"YOUR_TEXT")

hi sektor

still gives me a divide by zero error.
the calculation would look like this

100/0*0

i know the calculation is wrong and will not work, but was hoping iferror would fix it.

any other ideas?

cheers

dave

Then this way (if you want to hold string in result variable, then its type must be Variant):
Code:
``````On Error Resume Next
result = ....
If Err <> = 0 Then
result = "ERROR"  '// Your value for error indicator
End If
On Error GoTo 0``````

Hi

seem to have a probelm with this line of code
If Err <> = 0 Then

i have simplified the code to give me the same error

Code:
``c = 100 / 0 * 0``

so if we can get the above code to make c = "" or "error" then the rest will work

thanks for looking

dave

Ooops! Of course, it must be If Err <> 0

sektor

thats it mate.

many thanks

saved me doing a horrible formula workaround.

cheers

dave

You're welcome!

Replies
4
Views
243
Replies
11
Views
99
Replies
3
Views
132
Replies
3
Views
81
Replies
5
Views
195

1,203,507
Messages
6,055,809
Members
444,826
Latest member
aggerdanny

### 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.

### Which adblocker are you using?

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

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