IfError

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Normally I would use On Error but I want to find out why this doesn't work:

In column A, put some values in cell A1 to A6, ensuring at least one value is zero.

Code:
Dim MyArray() As Variant

MyArray() = Cells(1, 1).CurrentRegion.Value

Dim i, j

For i = 1 to 6

    j = Application.WorksheetFunction.IfError(1 / MyArray(i,1), 0)

Next i

When a value of 0 is encountered, I expected the IfError to return 0 but instead it shows an error message.

What is wrong?

Thanks
 
Last edited:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Because the division is performed in VBA, not by the IfError function.

This works:

Code:
    j = Evaluate("IfError(1 / " & MyArray(i, 1) & ", 0)")
 
Upvote 0
Thanks.

BTW, do you know the difference between:

IfError
If(Iserror)
IsError
 
Upvote 0
BTW, do you know the difference between:

IfError
If(Iserror)
IsError


This is where simple Google searches of these Excel functions can come in handy:

IfError - used to indicate what to return in the case that the formula returns an error (see: [/COLOR]https://www.techonthenet.com/excel/formulas/iferror.php[COLOR=#333333]

If(Iserror) - does the same thing as IfError, but used in older versions of Excel that do not have IfError (prior to Excel 2007)
IsError - simply returns a boolean value telling you if the formula returns an error or not (see: [/COLOR]https://www.techonthenet.com/excel/formulas/iserror.php[COLOR=#333333])
 
Last edited:
Upvote 0
Thanks


Would you say proper error handling with On Error is preferable over all of these?
 
Last edited:
Upvote 0
Would you say proper error handling with On Error is preferable over all of these?
It is probably dictated by the situation.

Personally, if I was trying to handle an error in VBA, I would probably use VBA error handling (On Error).
If I was placing a formula on the worksheet, then I would use one of the other Excel formula options.
 
Upvote 0
Another option would be to check if the array is zero first like
Code:
For i = 1 To 6
   If MyArray(i, 1) = 0 Then
      j = 0
   Else
      j = 1 / MyArray(i, 1)
   End If
   Debug.Print j
Next i
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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