iserror in VBA

Carlbacca

New Member
Joined
Oct 23, 2009
Messages
29
Can someone give me an example of an iserror function I can use in VBA.

basically I want vba to carry out a action if there is an error on a bit of code.

Thanks for your help
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
you can use on error goto <abcd> statement.

Not tested.
</abcd>
Code:
Your code......
.......

On error goto abcd



:abcd
error handling here
<abcd>
</abcd>
 
Upvote 0
Code:
Sub Error_Demo()
    
    On Error GoTo Error_Handler
    
    x = 1 / 0 '<---causes an error
    
    Exit Sub
    
Error_Handler:

    MsgBox "There was an error." & vbCr & "Error number: " & Err.Number, vbCritical, "Uh Oh!"
    
End Sub
 
Upvote 0
Hi

You will probably want to have a look at the On Error statement in VBA Help eg:

Code:
On Error Goto deal_with_error
 
'sample code:
 
Sheets("Data").UsedRange.Clear  '--- say this errors
 
'more code
 
On Error Goto 0  'turn off the previous On Error Goto line
 
Exit Sub
 
 
deal_with_error:
 
  'perhaps no sheet Data is in workbook?
  If Err = 9 Then
     Set ws = Worksheets.Add
     ws.Name = "Data"
     Resume   'returns the code execution to the original line which errored
  Else
     MsgBox "Some error occured - exiting sub"
     Exit Sub
End Sub
 
Upvote 0
Code:
Option Explicit

Sub TestError()
Dim lRow As Long, lError As Long
Dim sCurrentAction As String

On Error GoTo labError

sCurrentAction = "Calling Initialisation"
Call Initialise

sCurrentAction = "Finding a non existant string"
lRow = WorksheetFunction.Match("qwerty", ActiveSheet.Columns("A"), 0)

sCurrentAction = "Doing something else"
Call DoSomethingElse

Exit Sub
labError:
MsgBox "Error " & Err.Number & " whilst " & sCurrentAction
End Sub

Private Sub Initialise()
MsgBox "Initialising"
End Sub
Private Sub DoSomethingElse()
MsgBox "Doing something else"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,712
Members
449,093
Latest member
Mnur

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