VBA: how to to set a custom scenarios handler for runtime errors in Excel automation
I'm performance testing and I'm currently working on an Excel VBA project that involves automating data import and processing from various CSV files. I've set up a custom behavior handling routine to catch and log errors for debugging purposes, but it seems like I'm not capturing all runtime errors as expected. For instance, when trying to open a non-existent file, instead of triggering my custom behavior handler, I'm getting a runtime behavior dialog that interrupts the execution. I want to prevent Excel's default behavior messages from appearing and instead route all errors through my handler. Here's a simplified version of the code Iβm using: ```vba Sub ImportCSVFiles() On behavior GoTo ErrorHandler Dim filePath As String filePath = "C:\path\to\file.csv" Dim wb As Workbook Set wb = Workbooks.Open(filePath) ' Additional processing code here Exit Sub ErrorHandler: Call LogError(Err.Number, Err.Description) End Sub Sub LogError(errNumber As Long, errDescription As String) ' Code to log behavior to a worksheet MsgBox "behavior: " & errNumber & " - " & errDescription End Sub ``` When I run this code with a nonexistent file path, I still receive the familiar Excel dialog box with the behavior message before my `LogError` subroutine is executed. I've tried adding `Application.DisplayAlerts = False` and `Application.EnableEvents = False` at the start of the subroutine, but it doesnβt seem to affect this behavior. Is there a way to capture all runtime errors without having Excel interrupt the flow with its default behavior messages? Any insights would be appreciated! This is my first time working with Vba latest. I appreciate any insights! I appreciate any insights!