How To Debug Your VB - VBA Code
Instead of using message boxes and/or stopping and rerunning your code, using VB/VBa's Debug mode can save a lot of time; especially if you are just starting to learn the language and do not even know about this feature.
For example, if you are trying to format the text from cells in Excel you will display in a message box (MsgBox "text") and do not see the results you expect, you can stop the code right in its tracks and make all the changes to the code in real time, correcting and rerunning a problematic line(s) until you see that the text that would be displayed in the message box is just perfect.
Two ways to stop your code while leaving it "running" are...
1.
Type the word Stop on a line with no other code on it.
2.
Put your mouse in the line where you want it to stop and press F9 to set a Breakpoint.
You can also set a breakpoint by clicking in the grey margin to the left of the line.
When you run the code, execution will stop at that line and the line will be highlighted yellow (active).
You then Press F8 to continue running the code line by line, step by step.
You can also rerun a line(s) of code, that you may or may not have corrected, by right clicking on a previous line and choosing Set Next Statement.
You can also highlight any variable, right click it and then choose Add Watch and in the Watch Window that will appear at the bottom of the IDE (VB/VBA Editor) the variable's value can be seen as the code is running.
For a full tutorial on how to save time by debugging your code in Debug mode with images and more details, visit my How to Debug your code page at How to Debug Your Code.
For example, if you are trying to format the text from cells in Excel you will display in a message box (MsgBox "text") and do not see the results you expect, you can stop the code right in its tracks and make all the changes to the code in real time, correcting and rerunning a problematic line(s) until you see that the text that would be displayed in the message box is just perfect.
Two ways to stop your code while leaving it "running" are...
1.
Type the word Stop on a line with no other code on it.
2.
Put your mouse in the line where you want it to stop and press F9 to set a Breakpoint.
You can also set a breakpoint by clicking in the grey margin to the left of the line.
When you run the code, execution will stop at that line and the line will be highlighted yellow (active).
You then Press F8 to continue running the code line by line, step by step.
You can also rerun a line(s) of code, that you may or may not have corrected, by right clicking on a previous line and choosing Set Next Statement.
You can also highlight any variable, right click it and then choose Add Watch and in the Watch Window that will appear at the bottom of the IDE (VB/VBA Editor) the variable's value can be seen as the code is running.
For a full tutorial on how to save time by debugging your code in Debug mode with images and more details, visit my How to Debug your code page at How to Debug Your Code.