Excel VBA Break Point
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 to 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.
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 huge amount of code we need to test those lines of codes to proceed to the next level of coding to make sure our line of code 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 setting the breakpoint we can exit the VBA subprocedure as well.
Why do we need to use it?
Debugging is the 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 of a 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 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 Break Points in Excel VBA?
Breakpoints are very easy to add and remove from the line of codes. We can apply breakpoints in two ways.
- Pressing the F9 key by selecting the breakpoint line of code.
- Click on the left margin of the breakpoint line of code.
Now look at how to set a breakpoint by “Pressing the F9 key by selecting the breakpoint line of code”
Assume you have huge code something like the below line.
We have many lines of code here. Assume you want to set a breakpoint at the 6th line. Place a cursor on the 6th line.
After placing the cursor press F9 key.
As soon as you press an F9 key you can see a red highlighted line like the below.
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.
When you run the code, you can see in the left side until the A5 cell values are inserted.
On the right side, we can see the yellow-coloured line, it is an indication that line is about to execute that line of code but since we have set the breakpoint there it is paused there only.
Now look at how to set a breakpoint by using “Click on the left margin of the breakpoint line of code”
This method also works exactly the same way but applying is different.
As we have seen in the above image we needed to set the breakpoint at the 6th line. In this method we need to select the particular line of code here, rather we can just click on the left margin side of that particular line of code.
It will apply the breakpoint there.
Like this, we can set the breakpoint to pause the line of codes.
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.
You can use the F9 key to toggle the breakpoint on or off.
This has been a guide to Excel VBA Break. Here we learn how to apply breakpoint in VBA code using 2 methods 1) Press F9 Key 2) Click on the left margin of VBA Code along with examples and templates. Below are some useful excel articles related to VBA –