Raising Errors

The Err.Raise method can be used to generate run-time errors and can be used instead of the Error statement.
Using the Err object gives more information than the ERROR statement.
If you want to use sound to indicate when something is wrong or incorrect you can use the BEEP statement.


vbObjectError constant

A lot of documentation says if you want to generate a run-time error, you should add your number to the vbObjectError constant.

link - learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/raise-method 
Debug.Print VBA.vbObjectError '-2147221504
Debug.Print VBA.vbObjectError + 529 '-2147220975
Err.Raise VBA.vbObjectError + 529
Err.Raise VBA.vbObjectError + 529, , "BetterSolutions.com: Description"

This approach does work but in some cases, it can produce some confusing results.

link - stackoverflow.com/questions/56040569/why-do-we-need-to-use-vbobjecterror-constant-when-raising-user-defined-errors-in 

For example adding 515 to this constant, produces a native error.

Debug.Print VBA.vbObjectError         '-2147221504  
Debug.Print VBA.vbObjectError + 515 '-2147220989
Err.Raise VBA.vbObjectError + 515
Err.Raise VBA.vbObjectError + 515, , "BetterSolutions.com: Description"

Recommended Approach

For any user defined errors, the number should be less than 0 or between -10,000,000 and 0.
Instead of using the vbObjectError constant, use -10,000,000.
Err.Raise -10000000 + "Number", , "Description"

Debug.Print -10000000 + 515           '-9999485  

'This generates a custom error, with no description
Err.Raise -10000000 + 515

'This generates the same custom error, but this time, with a description
Err.Raise -10000000 + 515, ,"BetterSolutions.com: Description"

Err.Raise(Number [, Source] [, Description] [, HelpFile] [, HelpContext])

Number (Long)
Long, identifying the type of the error. Visual Basic errors range from 0 to 65535
The range 0 - 512 is reserved for system errors
The range 513 - 65535 is available for user defined errors
Source (String)
If this is not specified then the programming ID of the current project is used.
Description (String)
This the string that describes the error.
If the error can be mapped to a Visual Basic run-time error the string corresponding to the Number is used.
If there is no Visual Basic error corresponding to the number then "Application-defined or object-defined error" message is used. For example, "Err.Raise 1"
HelpFile
The full path of the Help file in which the error can be found
If unspecified then the fully qualified drive, path and file name of the Visual Basic help file is used.
HelpContext
The context ID that identifies the topic within the corresponding help file.
If omitted then the context ID for the error corresponding to the Number property is used if it exists.


Examples

Sub RaisingErrors() 
On Error GoTo ErrorHandler

    Err.Raise 1 'raises 1 - Application-defined or object-defined error
    Err.Raise 2 'raises 2 - Application-defined or object-defined error
    Err.Raise 3 'raises 3 - Return with GoSub
    Err.Raise 7 'raises 7 - Out of memory
    Err.Raise 513 'raises 513 - Application-defined or object-defined error
    Err.Raise 514 'raises 514 - Application-defined or object-defined error
    Err.Raise 2000 'raises 2000 - Application-defined or object-defined error
    Err.Raise 65535 'raises 65535 - Application-defined or object-defined error
    Err.Raise 655380000 '> 65535 AND < 655380000 raises "5 - Invalid Procedure Call or Argument"

    Err.Raise 0 'raises "5 - Invalid Procedure Call or Argument"
    Err.Raise -1 'raises " -1 - Automation Error"
    Err.Raise -10 'raises " -10 - Automation Error"
    Err.Raise -10000000 'raises " -10000000 - Automation Error"
    Err.Raise -2000000000 'raises "-2000000000 - Automation Error"
    Err.Raise -2147220000 'raises "-2147220000 - Automation Error"
    Err.Raise -2147220975 'raises "-2147220975 - Automation Error"

    Err.Raise -2147220977 '< 2147220977 raises a specific Automation Error"
    Err.Raise -2147220976 'raises "-2147220976 - Automation Error, The owner of the PerUser subscription"

    Err.Raise VBA.vbObjectError 'raises "-2147221504 - Automation Error, Invalid OLEVerb structure
    Err.Raise VBA.vbObjectError + 7 'raises "-2147221497 - Automation Error, Uninitialized object
    Err.Raise VBA.vbObjectError + 515 'raises "-2147220989 - Automation Error, A syntax error occurred trying"
    Err.Raise VBA.vbObjectError + 528 'raises "-2147220976 - Automation Error, The owner of the PerUser subscription"

    Err.Raise VBA.vbObjectError + 529 'raises "-2147220975 - Automation Error"
    Exit Sub

ErrorHandler:
    Call MsgBox(Err.Number & " - " & Err.Description)
End Sub

ERROR Statement

There is also an ERROR statement that can be used to generate errors.
This is only provided for backwards compatibility.


Calling Subroutine

It is possible to pass an error to its calling procedure.
If you want to pass an error back to the calling subroutine, you just need to remove the On Error statement at the top.

Public Sub ParentSub() 
    On Error GoTo ErrorHandler
    Call ChildSub()
    Exit Sub
ErrorHandler:
' handle the error here
End Sub

Public Sub ChildSub()
' if there is an error here, the error will be handled in ErrorHandler of ParentSub
End Sub

This handles the error in both places

Public Sub ParentSub() 
    On Error GoTo ErrorHandler
    Call ChildSub()
    Exit Sub
ErrorHandler:
' handle the error here
End Sub

Public Sub ChildSub()
    On Error GoTo ErrorHandler

    Exit Sub
ErrorHandler:
' handle the error here and pass it back to the ParentSub to handle it as well
    Err.Raise Err.Number
End Sub

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

Using Enum

If you want to raise your own specific errors and have code to handle these errors you could put everything into a dedicated class.
Define all your errors using an Enum.

Public enCustomErrors 
    lFirstError = -10000000 + 1024
    lSecondError
    lThirdError
End Enum

Use the vbObjectError built-in constant to ensure the custom error codes do not overlap with reserved/existing error codes.
The Err.Raise method can also take custom Description and Source parameters so it worth having a description associated with each error.

Private Const sDescription_FirstError As String = "This is the first type of error." 
Private Const sDescription_SecondError As String = "This is the second type of error."

Create a dedicated private method to raise each error:

Private Sub OnFirstError(ByVal source As String) 
    Err.Raise enCustomErrors.lFirstError, source, sDescription_FirstError
End Sub

Private Sub OnSecondError(ByVal source As String)
    Err.Raise enCustomErrors.lSecondError, source, sDescription_SecondError
End Sub

To raise the error, just call the correspnding subroutine.

Public Sub DoSomething() 
   If (True) Then
      Call OnFirstError("DoSomething")
   End If
End Sub

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