I have recently found the beautiful tool of debug code which helps a tons to find error and perfect my macro. I am however stuck with a loop that the debug does not want to go over. In other words, I would like to understand:
- how I can debug each line of a code,
- debug 1->n times (n being a finite number of times) the loop
- THEN go past the loop and debug the rest of the code.
I cannot find a way to debug past the loops. Could also someone try to explain me in general the most important things of the debug function?
Edit: Inserted example of code
I Still cannot go past the Nexts: if I press and hold F8, the code runs forever never pasting the loop. I have found a workaround which is to drag the highlighted F8 line code down to the desire location but it is annoying doing so when the code is long; also, if the code breaks I need to redrag it manually each time. I would like a way I could use that allows me to let’s say run the debug only from line 40 to 60 not before not after.
Here some tips to Debug in VBA:
- With F5 you execute the code until the end of the
- With F8 you execute a single line of code.
- By clicking on a line or pressing F9, you set a break point.
With these simple tips you can do almost everything with the Debug tool.
If you want to quickly get out of a loop, just set a break point on the immediate line of code after, and then press F5, like this:
You can use F8 to step through your code line by line
You could use BreakPoints which can be inserted by clicking on the line you want to break on i.e. the one after the Loop and press F9
or a very underrated function is
Debug.Assert. You can use this to write an expression that it will test against and break on when it evaluates to
Debug.Assert i < 50
The above will break on any value that is greater than 49.
Don’t forget to remove/handle this in your production copy though