VBA Break

Excel VBA BreakPoint

Break is used in VBA when we want a code to stop executing when the given criteria is met, or sometimes the code encounters an error which will show the incorrect value then we need our statement or code to break, there is an inbuilt statement in VBA for break and it is known as EXIT statement.

VBA Break is the pause button for the execution of the code. All the lines of code 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 of codes to proceed to the next level of coding to ensure our line of code is working as 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 step of code, we can set the breakpoint and test until the breakpoint and proceed from that breakpoint line by line. In some cases, without putting the breakpoint, we can exit the subprocedure as well.

Why do we need to use it?

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

For example, assume you have 100 lines of code, and you know for sure your first 25 lines of code is bug-free code. But the problem here is when we are testing the code, we need to run from the first line of code, we cannot straight run 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 your execution will be completed until the 26th line, and 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.

  • I am pressing the F9 key by selecting the breakpoint line of code.
  • Click on the left margin of the breakpoint line of code.
You can download this VBA Break Excel Template here – VBA Break Excel Template

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 on the left side until the A5 cell values are inserted.

On the right side, we can see the yellow-colored line, it is an indication 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 we have seen in the above image, we needed to set the breakpoint on the 6th line. In this method, we need to select a particular line of code 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 codesPause The Line Of CodesVBA Pause helps to pause a code from executing for a particular period. You can pause the VBA code for a specific period by using two functions, and those functions are “Wait” & “Sleep.”read more.

  • We can also set multiple breakpoints either by selecting the line of code and pressing the F9 key or click 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 has been a guide to Excel VBA Break. Here we learn how to apply breakpoint 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 –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion