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
 

Some videos you may like

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

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,271
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,494
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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,660
Messages
5,512,673
Members
408,909
Latest member
Burnrose

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top