Consulting

Results 1 to 3 of 3

Thread: How correctly check for error?

  1. #1
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location

    How correctly check for error?

    What do you suggest to change to check for errors?
      SexColNo = Application.Match("Pohlavi", SourceHeaders, 0)
      If Error = 2042 Then
        MsgBox "Header Pohlavi not found in source table."
        Err.Clear
      End If
      StateColNo = Application.Match("Stav", SourceHeaders, 0)
      If Error = 2042 Then
        MsgBox "Header Stav not found in source table."
        Err.Clear
      End If
      AgeColNo = Application.Match("Vek", SourceHeaders, 0)
      If Error = 2042 Then
        MsgBox "Header Vek not found in source table."
        Err.Clear
      End If

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    maybe something like this

    not tested and just done off the cuff

    Sub test()
        On Error GoTo ErrHandler
        
        SexColNo = Application.Match("Pohlavi", SourceHeaders, 0)
        If IsError(SexColNo) Then Err.Raise 1000, "My Prog", "Header Pohlavi not found in source table."
        
        StateColNo = Application.Match("Stav", SourceHeaders, 0)
        If IsError(StateColNo) Then Err.Raise 1001, "My Prog", "Header Stav not found in source table."
        
        AgeColNo = Application.Match("Vek", SourceHeaders, 0)
        If IsError(AgeColNo) Then Err.Raise 1002, "My Prog", "Header Vek not found in source table."
        
        'must be OK so do the rest
        
        Exit Sub
      
    ErrHandler:
        'optional
        Select Case Err.Number
            Case 1000
                'any special processing
            Case 1001
                'any special processing
            Case 1002
                'any special processing
        End Select
    
    
        MsgBox Err.Description
      
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location
    Thank you.

Posting Permissions

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