What is the Error “Can’t Execute Code in Break Mode”?

There are three modes in which the VBA IDE can operate: Run mode, Break mode, or Design mode.

The IDE is in design mode when you are writing code or designing a form. Run mode occurs when a procedure is running. Break mode is entered when a running procedure stops because of either an error in the code or a deliberate act by the programmer to run the code line by line to easily identify the source of an error.

In this article, you are going to learn how to run a VBA code in break mode and understand the origin of the error “can’t execute code in break mode.”

error message: can't execute code in break mode

Contents

Running a Procedure in Break mode

As we earlier mentioned, Break mode pauses the execution of a code, and allows you to edit it. In break mode properties and values are held so that you can analyses their current state. Break mode can be activated by the following ways:

1. Select Break from the Run menu or Click + Break or press the Pause button while the procedure is running.

select break from the run menu

2. Insert a Stop statement in your code.

stop statements in code

3. Insert a Breakpoint in your code. There are many ways through which you can insert a breakpoint in a code. The easiest one are: click in the light grey area to the left of the line where you want a break or you place the cursor on the desired line and hit on the F9 key.

insert breakpoints in your code

4. Use Step into on the debug toolbar or press F8 to execute one line of code at a time starting from the location of the break.

Note that the VBA IDE goes automatically on a Break mode when it encounters an error while running a procedure. In this case, it will stop the execution of the procedure at the problematic code and highlight that code in yellow.

The Cause of the “Can’t Execute Code in Break Mode” Error and How to Fix It

One of the most common reason for this error is that you tried to run code from the Macro dialog box when the same code that was launched in Visual Basic was suspended in break mode accidentally, intentionally or because of an error in the coding. To solve that, you should continue running the suspended code, or terminate its execution before running the code from the Macro dialog box.

Example

In this example we are going to write a small procedure to fill cells A1 to A50 with values from 1 to 50.

Sub Fillcells()
    Dim i As Integer
    For i = 1 To 50
    Range("A & i).Value = i
    Next i
End Sub

Note that our procedure/Macro is called “Fillcells”. This code will run without stop to then end, but this is not our intention. What we want is to insert a break in the code and try running it through the Macro dialog box to see the result.

To do that we first have to add a Stop statement or a break point as follows:

Sub Fillcells()
    Dim i As Integer
    For i = 1 To 50
    Range("A & i).Value = i
    Stop
    Next i
End Sub

With this second code you need to hit the Run button fifty times! (just press it once) To write all the values. For our example, just press it once to get to this:

highlighted line of code in break mode

Now that we are in a break mode as seen by the highlighted line in yellow let’s try to run the same procedure named Fillcells from the Macro dialog box.

run procedure from dialog box

Once you press the Run button, you will get the “Cannot execute code in break mode as seen below, just because the procedure still running in VBA is in Break mode. So you have to stop that one before coming back to the Macro dialog box to run the macro.

can't execute in break mode error popup

Hope it was not too difficult to understand.

One thought on “What is the Error “Can’t Execute Code in Break Mode”?”

Leave a Reply

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