Everything You Need to Know About Break Mode in VBA

Contents

Modes in VBA

VBA operates in 3 modes:

  1. Run mode: The execution of code.
  2. Break mode: A halt in the execution especially for debugging purposes.
  3. Design mode: The state of the VBA Editor in which code is typed.

The meaning of “Runtime” or “Run mode” and related shortcut keys

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:

  1. 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.
  2. 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.
  3. 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.

  1. Using breakpoints
  2. Ctrl + Break shortcut or F9
  3. Stop statements

Some errors can automatically get us into break mode. We can continue with execution after fixing them.

Breakpoints

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.

Setting a breakpoint

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.

Finding break mode in the VBA editor menu

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.

Using the stop statement to enter break mode

 

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.

Break mode
Can't execute code in break mode (editor in break mode)

In the case below, we have entered into a break mode using breakpoint.

Break point for break mode in the code
Can't execute code in break mode (break point)

Conclusion

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.

See also:

One thought on “Everything You Need to Know About Break Mode in VBA”

Leave a Reply

Your email address will not be published. Required fields are marked *