Skip to main content
Nintex Community Menu Bar
Knowledge Base

Follow best practices to run Custom Macro to Run using Nintex RPA

  • July 3, 2024
  • 0 replies
  • 19 views

Forum|alt.badge.img

Topic

To code effectively in Microsoft Visual Basic for Applications (VBA) with proper error handling and to ensure no error message dialogues are shown, follow these best practices.


Instructions 


 1. Use `On Error` Statements for Error Handling
Implement `On Error` statements to manage errors gracefully.

- On Error Resume Next: Continues execution with the next line of code after an error occurs.
- On Error GoTo [label]: Jumps to a specified label when an error occurs.

 2. Create a Centralized Error Handling Routine
Design a centralized error handler to handle errors uniformly.

===vba
Sub ExampleSub()
    On Error GoTo ErrorHandler
    ' Your code here

    Exit Sub
ErrorHandler:
    ' Handle the error
    Resume Next ' Or handle accordingly
End Sub
===

 3. Use `Err` Object to Capture Error Details
The `Err` object provides information about runtime errors.

===vba
Sub ExampleSub()
    On Error GoTo ErrorHandler
    ' Your code here

    Exit Sub
ErrorHandler:
    Debug.Print "Error " & Err.Number & ": " & Err.Description
    Resume Next
End Sub
===

 4. Avoid Unnecessary Error Dialogues
Set `DisplayAlerts` to `False` to prevent Excel from showing alert messages.

===vba
Sub ExampleSub()
    On Error GoTo ErrorHandler
    Application.DisplayAlerts = False

    ' Your code here

    Application.DisplayAlerts = True
    Exit Sub
ErrorHandler:
    Application.DisplayAlerts = True
    Resume Next
End Sub
===

 5. Validate Inputs and Conditions
Always validate inputs and preconditions before executing code that might fail.

===vba
Sub ExampleSub()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    If Not ws Is Nothing Then
        ' Your code here
    Else
        ' Handle case where worksheet is not found
    End If
End Sub
===

 6. Clean Up Resources in Case of Errors
Ensure resources like files or database connections are properly closed even if an error occurs.

===vba
Sub ExampleSub()
    On Error GoTo ErrorHandler
    ' Open file or connection here

    ' Your code here

    ' Close file or connection
    Exit Sub
ErrorHandler:
    ' Close file or connection if open
    Resume Next
End Sub
===

 7. Log Errors for Troubleshooting
Log errors to a file or a worksheet for later analysis.

===vba
Sub LogError(ByVal ErrorMessage As String)
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("ErrorLog")
    
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
    
    ws.Cells(lastRow, 1).Value = Now
    ws.Cells(lastRow, 2).Value = ErrorMessage
End Sub

Sub ExampleSub()
    On Error GoTo ErrorHandler

    ' Your code here

    Exit Sub
ErrorHandler:
    LogError "Error " & Err.Number & ": " & Err.Description
    Resume Next
End Sub
===

 8. Test Error Handling Thoroughly
Test your error handling code by simulating errors to ensure it works as expected.

 Example: Putting It All Together

===vba
Sub ProcessData()
    On Error GoTo ErrorHandler
    Application.DisplayAlerts = False
    
    ' Example of data processing code
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Data")
    
    ' Check if worksheet exists
    If ws Is Nothing Then GoTo Cleanup
    
    ' Your data processing code here
    
Cleanup:
    Application.DisplayAlerts = True
    Exit Sub
    
ErrorHandler:
    LogError "Error " & Err.Number & ": " & Err.Description
    Resume Cleanup
End Sub

Sub LogError(ByVal ErrorMessage As String)
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("ErrorLog")
    
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
    
    ws.Cells(lastRow, 1).Value = Now
    ws.Cells(lastRow, 2).Value = ErrorMessage
End Sub
===

These practices will help you manage errors effectively in VBA without displaying error message dialogues to the user.