Everything You Need to Know About Break Mode in VBA
Modes in VBA
VBA operates in 3 modes:
- Run mode: The execution of code.
- Break mode: A halt in the execution especially for debugging purposes.
- Design mode: The state of the VBA Editor in which code is typed.
Runtime is simply the duration during which code runs or executes after being compiled. Runtime duration and speed can be controlled in VBA programming using the F5 and F8 keys.
F5 can be used to initiate or execute a full run of a sub procedure.
F8 is used to execute the lines of code step-by-step.
What is break mode?
Break mode is a state of code execution where it has been halted for some reason. In general, code execution can be halted for several reasons — allow me to explain. Here’s why you’d want to go into break mode:
- Re-execute the previous steps in order to observe the execution. This can be done by dragging the yellow cursor along the margin to the line you want to re-execute. Re-execution of certain code might require you to stop execution altogether and then start execution all over again.
- Make minor changes in previous steps and re-execute any piece of code that were run before. If major changes that affect the logic of the code are made, VBA may require us to end the execution and start it all over. There might even be an alert which automatically ends the execution if we click on its “Okay” button. Any more advanced changes will be allowed only if the execution is ended.
- Flow of execution can be observed and confirmed to make sure it is meeting our expectations.
How to enter break mode
We can explicitly enter break mode using any one of the 3 methods explained below.
- Using breakpoints
- Ctrl + Break shortcut or F9
- Stop statements
Some errors can automatically get us into break mode. We can continue with execution after fixing them.
A breakpoint is an element that is used to halt the execution of your code at any specific position or line, entering you into break mode in order to debug the code. In VBA, the line is colored in yellow for identification purposes. A breakpoint could be compared a speed bump on the road.
Here is a picture of a program execution halted using a breakpoint. A breakpoint can be inserted using the Debug menu or the shortcut key F9 after placing the cursor on the desired line.
Using the Ctrl+Break option
To explicitly enter break mode for debugging purpose, we can use Ctrl + Break anytime before or during execution. There is also a menu for this as shown in the picture below.
Using the Stop statement
Just like the Wait statement, we have a “Stop” statement in VBA that can halt execution and take the user into break mode. Check out the example below.
Errors that lead to break mode during execution
There are some run time and syntax errors that may lead to break mode while your code is getting executed. Some number of errors can be fixed on the spot, and we can click on “Continue” button for the execution to continue.
Below are some examples of situations with errors in break mode.
Running code from Macro dialog box while the VBA editor is already in break mode.
In the case below, we have entered into a break mode using breakpoint.
Break mode in VBA is used for debugging and fixing errors. If we need to continue with execution, we should either click on the play button or press F5 or F8. Or we should just end the program’s execution and start a fresh run of the code.
Executing a sub procedure from the Macro dialog box while the code is in Break mode will only lead to the aforementioned error. So you need to be cautious to avoid it.
One thought on “Everything You Need to Know About Break Mode in VBA”