PDA

View Full Version : [SOLVED:] How correctly check for error?



vangog
12-26-2021, 02:34 PM
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

Paul_Hossler
12-26-2021, 04:09 PM
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

vangog
12-26-2021, 04:56 PM
Thank you.