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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

agrawal.meet

New Member
Joined
Jun 30, 2010
Messages
38
you can use on error goto <abcd> statement.

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

On error goto abcd



:abcd
error handling here
<abcd>
</abcd>
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,452
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
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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
 

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,526
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
 

Forum statistics

Threads
1,172,041
Messages
5,878,862
Members
433,380
Latest member
Hadfield

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
Top