Err.Source

This approach allows you to have a custom error-numbering strategy without fear of conflicting with native errors (or errors generated by other workbooks or applications.)
link - stackoverflow.com/questions/69370099/what-numbers-should-be-used-with-vbobjecterror/70283901#70283901


Public Sub Test_Handler() 
   On Error GoTo ErrorHandler
          
   Debug.Print 1 / 0 ' 11, Division-by-zero
   Kill "C:\Doesn't-Exist.txt" ' 53, File not found
   Err.Raise 11, "Custom" ' 11, User cancelled
   Err.Raise 53, "Custom" ' 53, User paused
   Exit Sub

ErrorHandler:
   Call GlobalHandler
   Resume Next
End Sub

Public Sub GlobalHandler()
Dim sDesc As String
          
   Select Case Err.Source
      Case "Custom"
         Select Case Err.Number
            Case 11: sDesc = "User Cancelled the process"
            Case 53: sDesc = "User has paused"
         End Select

      Case Else
         sDesc = Err.Description
   End Select
          
   Call MsgBox(sDesc)
End Sub

© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext