Worksheetfunction.IFERROR

Roncondor

Board Regular
Joined
Jun 3, 2011
Messages
79
Hi

I want it to work just like it does in regular Excel - but it always gives me an error message (see the attached picture)

I am enclosing a VERY simple sample macro (see below)

When I run it, I get a "division by zero" instead of "X" becoming a 0

This seems simple, but I cannot figure it out

THANK YOU!

Sub TestIferror()

Dim X As Integer

X = WorksheetFunction.IfError(1 / 0, 0)

End Sub
 

Attachments

  • iferror.JPG
    iferror.JPG
    31.7 KB · Views: 3

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I believe you need the error situation to be in a cell, say, A1 is =some_value/0 which means A1 contains the divide by 0 error.
Then, WorksheetFunction.IfError(Range("A1"),0) should work.
 
Upvote 0
The problem is that VBA evaluates the 1/0 before it does the rest of the line, and when it gets the error it doesn't even try to finish the rest of the line. There are a couple ways to do this. You can use EVALUATE like this:

VBA Code:
x = Evaluate("IFERROR(1/0,0)")

or you can use some more VBA specific type coding:

VBA Code:
    On Error Resume Next
    x = 0
    x = 1 / 0

In this case, you set x to the default value. Then perform the calculation. If that creates an error it just goes to the next line leaving the default value intact.
 
Upvote 0
I believe you need the error situation to be in a cell, say, A1 is =some_value/0 which means A1 contains the divide by 0 error.
Then, WorksheetFunction.IfError(Range("A1"),0) should work.

Thanks - but that limitation of Excel defeats the purpose - as the whole reason that VBA has worksheet.functions is to avoid using cells.

In the end, for my actual macro, I have a cell in the spreadsheet that performs my calculation within which I have the IFERROR, and VBA checks that cell
 
Upvote 0
The problem is that VBA evaluates the 1/0 before it does the rest of the line, and when it gets the error it doesn't even try to finish the rest of the line. There are a couple ways to do this. You can use EVALUATE like this:

VBA Code:
x = Evaluate("IFERROR(1/0,0)")

or you can use some more VBA specific type coding:

VBA Code:
    On Error Resume Next
    x = 0
    x = 1 / 0

In this case, you set x to the default value. Then perform the calculation. If that creates an error it just goes to the next line leaving the default value intact.

Thank you for the response

Your first answer probably would work

The problem with the second is that I need to check the result to determine what I would do = not just continue with the macro
 
Upvote 0
This feeds into a much larger topic of error handling. Instead of the On Error Resume Next, you could have an On Error Goto ErrorHandler: where you do something, then use a Resume Label01: to get back to the main code. Or use the Err method like this:

VBA Code:
    On Error Resume Next
    x = 0
    x = 1 / 0
    If Err.Number > 0 Then
        Debug.Print "Error code:  " & Err.Number
        Debug.Print "Description: " & Err.Description
    End If

It just depends on your macro.
 
Upvote 0

Forum statistics

Threads
1,215,631
Messages
6,125,905
Members
449,273
Latest member
mrcsbenson

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