Saturday, May 4, 2013

Avoiding On Error Resume next statements in VBA what is the road ahead then?

Avoiding Error statements in VBA what is the road ahead then?

Blank vs "" vs 0
Keeping there nothing
Keeping there ""
Formula giving "" or 0

NA is error

All of them have their importance and implcation.

0 is different from isblank ... iszero.... is function... is date ... etc

These might become useful at times...

Also you can use 0 1 + etc while dealing with boolean which means true false 1 0 & and 

Explicitly handling error and not using error handlers is the key..

Sub errorp1()

Dim i, j As Double
For i = 1 To 5 Step 1
j = 1 / (i - 2)
On Error Resume Next
MsgBox j
'ignore the error and move ahead keeping last value
Next i
End Sub

Sub errorp2()

Dim i, j As Variant
For i = 1 To 5 Step 1
j = 1 / ((i - 2) * (i - 3))
MsgBox j
On Error GoTo ER
GoTo ER2
On Error GoTo 0
MsgBox "error"
Resume Next

Next i

End Sub

'On Error GoTo 0 disables any error trapping currently present in the procedure.
'On Error GoTo -1 clears the error handling and sets it to nothing which allows you to create another error trap.

Sub ErrorTest()

Dim dblValue As Double

On Error GoTo ErrHandler1
dblValue = 1 / 0
MsgBox "Exception Caught"
On Error GoTo -1 'Comment this line to check the effect
On Error GoTo ErrHandler2
dblValue = 1 / 0
MsgBox "Again caught it."

End Sub
Sub ErrorTestUserHandler()

Dim dblValue As Double

For lngloop = 1 To 10
On Error GoTo ErrHandler
dblValue = 1 / 0
MsgBox "Caught it."

On Error GoTo -1 'Comment this line and run it again to see the effect

End Sub

Join our VBA for financial engineering course ( & get 15% discount. Ask for discount code, email -

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.