vba iferror

SQUIDD

Well-known Member
Joined
Jan 2, 2009
Messages
2,104
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
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)

thanks in advance
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")
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 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
 
Upvote 0
Ooops! Of course, it must be If Err <> 0
 
Upvote 0
sektor

thats it mate.

many thanks

saved me doing a horrible formula workaround.

cheers

dave
 
Upvote 0
You're welcome!
hi.gif
 
Upvote 0

Forum statistics

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