PDA

View Full Version : Catching fatal VBA errors



Better_Days
05-18-2018, 05:49 AM
I've a sheet that receives data from another program and now and then it fatally crashes, I'm thinking it's due to some data not being present that it's expecting. It's receives betting data and once a non runner is declared I guess price data for it is missing. I'm not sure where it's falling over though as it just freezes and has to be shut down and reopened. I have on error resume next in a few places but still crashes.

Is there any code I can add to the overall sheet that would catch where the sheet is falling over rather than trying to try and pin which code in all the various modules is crashing. It currently fires everything off using a Worksheet_Change event to call routines in various modules at te moment.

SamT
05-18-2018, 11:17 AM
First remove or comment out all On Error and other Error handlers.
Second in the VBA Menu >> Tools>> Options>> General Tab, Set "Break on all Errors"

Make sure that "Option Explicit" is at the top of all modules. Only then, Compile the code. Fix all mistakes that finds.

When receiving data from the betting program, (Which one?) and it breaks, Set some Watches to see what values various objects, variables, etc contain. Right Click the item you want to Watch. You can also hover the mouse over many words to see that value.

If you right click the vertical border on the left side of the Code Pane, you can set the next statement to run, which means you can rerun the code from there... Handy to check edited code during the same process. Or skip some code, (not recommended) depending where you set the Next line to run.

You can also set Break Points, which will pause the code at that line. Pressing F8 will run the code one line at a time. Pressing F5 will run the code until it meets a BreakPoint, or fails... or finishes.