Page 1 of 1

Error is thrown before the error handler can handle it

Posted: Mon Apr 22, 2019 6:50 am
by preechaw
Hi.

I tried to emulate Excel IFERROR function.

I tried to do it in two ways:

[First]

Code: Select all

Function IFERROR(Expression as Variant, DefaultValue as Variant)
	If ISERROR(Expression) Then
		IFERROR = DefaultValue
	Else
		IFERROR = Expression
	End If
End Function
[Second]

Code: Select all

Function IFERROR(Expression as Variant, DefaultValue as Variant)
	On Error Goto ERRHANDLER
	IFERROR = Expression
	Exit Function
ERRHANDLER:
	IFERROR = DefaultValue
End Function
The problem is when the expression is erroneous, it's shown in the cell and the error handling inside the functions doesn't have a chance to handle it.

Is there any way to achieve this?

Thanks.

Re: Error is thrown before the error handler can handle it

Posted: Mon Apr 22, 2019 8:47 am
by JeJe
Have you seen this thread:

viewtopic.php?t=74980&p=340000

Re: Error is thrown before the error handler can handle it

Posted: Mon Apr 22, 2019 8:53 am
by preechaw
I realize it can be done that way.
If the workbook contains so many places using IFERROR, it's time-consuming to edit all of them (in case their occurrences are not consecutive and cannot be copied/pasted easily).
Using a VBA function will be more efficient.

Now my concern is not only how to make an equivalence of IFERROR.
It's now how to trap expression errors in the function.
The current behavior is the error is displayed before the function works.

Re: Error is thrown before the error handler can handle it

Posted: Mon Apr 22, 2019 10:12 am
by Zizi64
I tried to emulate Excel IFERROR function.
The LibreOffice Calc has IFERROR() function, but the Apache OpenOffice Calc has not.

Try it, and use it.

Re: Error is thrown before the error handler can handle it

Posted: Mon Apr 22, 2019 1:07 pm
by JeJe