+ Reply to Thread
Results 1 to 3 of 3

Error Handling not working

Hybrid View

  1. #1
    Registered User
    Join Date
    07-09-2017
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    5

    Error Handling not working

    Hi

    I have a Error Handling routine that is not working. Could someone please have a look and see where I went wrong.

    Basically, when the error occurs it does what it is supposed to do which is display message box, focus and clear text box. However, when there is not an error the main code executes the way it should but still displays the error message box.

    Thanks

    Private Sub cmdSearch_Click() 
        On Error Goto ErrHandler 
         
         
        If Me.txtPID.Value = "" Then 
            MsgBox "Please enter ID.", vbCritical 
            Me.txtPID.SetFocus 
            Exit Sub 
        End If 
         
         
        lblName = Application.VLookup(CLng(txtPID.Value), Worksheets("Lists").Range("M:P"), 2, 0) 
        lblStat = Application.VLookup(CLng(txtPID.Value), Worksheets("Lists").Range("M:P"), 4, 0) 
        lblGender = Application.VLookup(CLng(txtPID.Value), Worksheets("Lists").Range("M:P"), 3, 0) 
        lblRef = txtPID.Value & Format(txtDate.Text, "ddmm") & Application.VLookup(cboTime.Value, Worksheets("Lists").Range("F:G"), 2, 0) 
        Exit Sub 
         
         
    ErrHandler: 
        MsgBox "Not a current ID. Please try again!", vbCritical 
        txtPID.Value = "" 
        Me.txtPID.SetFocus 
    End Sub

  2. #2
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,347

    Re: Error Handling not working

    Are you sure it is not just that you are getting an error from one of those lines?

    comment out the "on error" line, run it on some supposedly good data and tell us what like it errors on and what message.
    If you want something done right... find a forum and ask an online expert.

    Time flies like an arrow. Fruit flies like a banana.

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,303

    Re: Error Handling not working

    Hi no1uno,

    Is your code behind a worksheet or in a Module? If it is behind a worksheet, insert a real module and put the code in there. Try the below.
    Sub cmdSearch_Click()
        On Error GoTo ErrHandler
          
        If Me.txtPID.Value = "" Then
            MsgBox "Please enter ID.", vbCritical
            Me.txtPID.SetFocus
            GoTo Ender
        End If
        
        lblName = Application.VLookup(CLng(txtPID.Value), Worksheets("Lists").Range("M:P"), 2, 0)
        lblStat = Application.VLookup(CLng(txtPID.Value), Worksheets("Lists").Range("M:P"), 4, 0)
        lblGender = Application.VLookup(CLng(txtPID.Value), Worksheets("Lists").Range("M:P"), 3, 0)
        lblRef = txtPID.Value & Format(txtDate.Text, "ddmm") & Application.VLookup(cboTime.Value, Worksheets("Lists").Range("F:G"), 2, 0)
        GoTo Ender
         
    ErrHandler:
        MsgBox "Not a current ID. Please try again!", vbCritical
        txtPID.Value = ""
        Me.txtPID.SetFocus
        On Error GoTo 0
    Ender:
    
    End Sub
    http://www.cpearson.com/excel/Scope.aspx
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Error handling inside error handling
    By grantastley in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-06-2015, 03:43 AM
  2. RUN TIME ERROR '1004' error handling help needed
    By skop89 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-09-2014, 12:54 PM
  3. [SOLVED] Error Handling: Creating code to display error messages
    By Student1990 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-01-2013, 02:21 PM
  4. Unusual error-handling results in previously working code written for Excel 2002
    By guindi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2007, 02:27 PM
  5. [SOLVED] Error Handling - On Error GoTo doesn't trap error successfully
    By David in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-16-2006, 02:10 PM
  6. [SOLVED] Error handling with a handling routine
    By ben in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-15-2005, 11:06 AM
  7. [SOLVED] error handling not working
    By Mike Macgowan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-14-2005, 06:06 PM

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