+ Reply to Thread
Results 1 to 4 of 4

simplest VBA code to exit a sub using On Error?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-23-2005
    Posts
    253

    simplest VBA code to exit a sub using On Error?

    What is the simplest VBA code to exit a sub using On Error?

    Thanks, mikeburg

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Do uou mean like this

    Option Explicit
    
    Sub test()
    
       On Error GoTo test_Error
    
        'Your code
    
       On Error GoTo 0
       Exit Sub
    
    test_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure test of Module Module1"
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Mikeburg,

    The easiest thing to do is jump to a line label at the end of your code. Place the line label before the End Sub statement.
    Private Sub ErrTest()
      On Error GoTo Fault
        N = 1/0
    
      ' The line label must always start in column 1, and end with a colon (:)
    
    Fault:
      If Err.Number <> 0 Then
         MsgBox Err.Description
      End If
    End Sub
    Sincerely,
    Leith Ross

  4. #4
    Forum Contributor
    Join Date
    06-23-2005
    Posts
    253
    I see what you mean.

    I thought something like On Error Exit Sub?

    Thanks, mikeburg

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1