VBA Break

Last Updated :

21 Aug, 2024

Blog Author :

Edited by :

Ashish Kumar Srivastav

Reviewed by :

Dheeraj Vaidya, CFA, FRM

Table Of Contents

arrow

Excel VBA BreakPoint

A break is used in VBA when we want a code to stop executing when it meets the criteria. Otherwise, sometimes the code encounters an error that will show the incorrect value, then we need our statement or code to break. There is an inbuilt statement in VBA for the break, known as the EXIT statement.

VBA Break is the pause button for the execution of the code. All the lines above the breakpoint will run until it finds the breakpoint in the code.

For your information, below is the sample image of the breakpoint line.

VBA-Break example

In the above image, a red line indicates the breakpoint.

The discovery of the power of VBA comes when we write huge code to execute big projects to get the job done. When we write a considerable amount of code, we need to test those lines to proceed to the next level of coding to ensure our line of code works per our objective.

When you test huge code, it takes a lot of time to step through each line. So, to run fast to the next level or code step, we can set the breakpoint and test until the breakpoint and proceed from that breakpoint line by line. Sometimes, we can exit the subprocedure without putting the breakpoint.

Why do we need to use it?

Debugging is part and parcel of coding in any language. Debugging allows us to find the loopholes in the code and fix them. While debugging a large amount of code, we need to run through bug-free lines of code without wasting much time. Bug-free code is without any error codes.

For example, assume you have 100 lines of code, and you know your first 25 lines of code are bug-free. But the problem here is when we are testing the code, we need to run from the first line of code. We cannot run straight into the 26th line. So in these cases, we need to set the breakpoint and run through the first 25 lines of code quickly.

By setting the VBA breaking point, you can press the F5 key to run the macro code, and it will complete your execution until the 26th line. Since we applied the breakpoint, it will stop there. From that line onwards, we can debug the code.

How to Apply BreakPoints in VBA?

Breakpoints are very easy to add and remove from the line of codes. We can apply breakpoints in two ways.

  • We are pressing the F9 key by selecting the breakpoint line of code.
  • Click on the left margin of the breakpoint line of code.

Method #1

Now, look at how to set a breakpoint by “Pressing the F9 key by selecting the breakpoint line of code.”

  1. Assume you have a considerable code, something like the below line.


    apply break points example1.1

  2. We have many lines of code here. Assume you want to set a breakpoint on the 6th line. Place a cursor on the 6th line.


    apply break points example1.2

  3. After placing the cursor, press the F9 key.


    apply break points example1.3

  4. As soon as you press an F9 key, you can see a red highlighted line like the below.


    apply break points example1.4

  5. Now, if you run the code, it will execute until it finds the red line or paused line we have created by pressing the F9 key.


    apply breakpoints example1.5

When you run the code, you can see it on the left side until it inserts the A5 cell values.

On the right side, we can see the yellow-colored line, indicating that the line is about to execute that line of code. But, since we have set the breakpoint, it is paused there only.

Method #2

Now, look at how to set a breakpoint using “Click on the left margin of the breakpoint line of code.”

This method also works the same way, but applying is different.

  • As seen in the above image, we needed to set the breakpoint on the 6th line. In this method, we must select a particular code line here. Instead, we can click on the left margin side of that specific line of code.
apply breakpoints example 2.1
  • It will apply the breakpoint there.
apply breakpoints example 2.2

Like this, we can set the breakpoint to pause the line of codes.

  • We can also set multiple breakpoints by selecting the line of code and pressing the F9 key or clicking on the left margin of the line of code.
apply breakpoints example 2.3

You can use the F9 key to toggle the breakpoint on or off.

Recommended Articles

This article has been a guide to Excel VBA Break. Here, we learn how to apply breakpoints in VBA code using two methods 1) Press F9 Key 2) Click on the left margin of the VBA Code along with examples and a downloadable Excel template. Below are some useful Excel articles related to VBA: -