Debugging through Loops - excel

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.
Example of the code

Here some tips to Debug in VBA:
With F5 you execute the code until the end of the Sub or Function
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 Falsee.g.
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

Related

MS. VBA Debugging "Run to Cursor" not working

Hello all,
this has me quite baffled as I dont even know how to even search for a solution to this.
Basically the problem is that when I'm debugging, after I stop the code in a breakpoint, the debugger can only go line by line and seems to not be able to "Run" (with F5, just continuing executing the code) or "Run to Cursor" to execute the code until the position the cursor is.
This makes of course very difficult the debugging, especially in cases where you just want to skip over a for-loop with 300000 iterations and not go through it one by one...
I have somehow found a workaround, although not really: I can set a breakpoint at some point down the code and select "Step Out" (Ctrl+Shift+F8) and the program will execute until the next breakpoint. This is far from ideal as I need to be setting and removing the breakpoints instead of just placing the cursor where I wanted the program to go and click Ctrl+F8. This operation is something I probably do hundreds of times while I am debugging and now I just go so much slower.
I wondered if any of you encountered this problem and know how to fix it or change this behaviour?
I should mention that the issue came when I changed my computer for a new one. I'm using Microsoft Visual Basic for Applications 7.1 within MS Excel in Microsoft Office 365

Locating correct run-time error line in VBA

I am getting a runtime error in VBA. I have no error-handling and don't really wish to employ this unless necessary.
When I get the run-time error window option to End, Debug or Help, I press Debug. The code editor window opens but in my case it highlights in yellow a line "Load Frm" - which is obviously tells me the error has occurred somewhere in the process of loading the form, but I then have to F8 throughout the whole series of routines which are called until I reach the line that has actually caused the error. This can take a long time! Am I missing something?
I use Google Translator, so there may be some inaccuracies, but I think you understand.
In the "Tools" menu of the VBA editor select "Options"
Select the "General" tab
In the "Error detection" frame select "Abort in class module" and confirm with OK.
In order to quickly find the line with the actual error you can use the following hot keys:
Shift+F8 when pressed on a line containing a procedure will run the whole procedure (even if it contains several lines inside) and then stop at the next line.
If you have stepped inside a procedure you can press Ctrl+Shift+F8 to run everything inside it and go to the next line of the outer procedure. In other words if Sub A at line 5 calls Sub B and you stepped into Sub B, pressing Ctrl+Shift+F8 will run everything in Sub B and return to line 6 of Sub A.
Don't forget about breakpoints, they're very useful.

Spyder: How to run all cells above current cell?

I am in heavy debugging mode where I have to restart the kernel fairly frequently, and am making heavy use of lots of cells in Spyder. When I am about 10 cells in and this happens, it would be really nice to be able to just run all cells above the current cell, sort of like you have in Jupyter.
I know I can manually go through and run cells 1-9 with Shift-Enter, or I can select all the code manually and hit F9. Is there a shortcut?
(Spyder maintainer here) There's no shortcut and no way to run all cells above or below the current one.
However, I think this is a very valid concern, so please open an issue in our issue tracker so we don't forget to implement it in a future release.
I usually just create a syntax error where I want to stop execution and then press F5.
EDIT:
It just occurred to me that this isn't quite right. A syntax error will prevent execution from starting. What I actually do is provoke a NameError:
NameError: name 'stop' is not defined

How to step back a line of code with Android Studio debugger

I am new to the debugger. When I step over a line of code, I was wondering how to step back. Now I'm realizing that the code can't be executed backwards? Do I have to restart the activity in debug if I want to step back to old lines?
Also, if you don't mind, what is the force-step-into command?
Don't think that is possible. Android works like all other debuggers, which are more or less waterfalls over the code. You can't really step backwards, but thats where breakpoints come in. Place a break point before the line you wanted to step backward on and rerun your app, it'll keep going until it hits that line.
To explain Force-Step-Into I'm going to explain Step-Into/Over first. Step-Over sees the line you want, and steps over it, stopping at the line in the same file after the function call was made. Step Into on the other hand steps into the function call and stops on the first line of that function call, could be in a new file or a new location. In Android, there are a lot of functions that are considered "Black Boxes" where you don't really care what they do since they most probably don't affect the code (Like the Log.d function). Force Step Into allows you to step into those if you'd want to.
Source: https://www.quora.com/How-would-you-explain-in-one-line-about-Step-over-Step-into-Force-Step-into-and-Step-out-of-Android-studio
Notice that you can set conditional breakpoint, i.e. breakpoint will stop execution only if x == 57 (so you don't have to skip manually all 56 breaks). Very useful. Just set breakpoint, click with right button on it and set your condition.
May be Frames can help to check method() calling stack in debugger window
YES, you can!
According to the Android Dev Summit in 2019, now you can step back while debugging with Android Studio!
Check out the live demonstration.
All you need to do is to press the Drop Frame button in your debug view, and then Resume Program. Then you will automatically get back to the last break-point.
Note: the device should run at least Android 10.
I don't think that is possible, like #OmegaNalphA mention, but you can have a look on the stack trace and see the order your code is executed.
StackTraceElement trace = new Exception().getStackTrace();
Log.d("myapp", trace.toString());

Control Break out of Infinite Loop In 2010 (2013) Excel VBA

If I write code creating an infinite loop, with my new Excel, the Ctrl + Break no longer works. Neither does the Esc key, etc.
I've looked all over the web and it appears that Microsoft has a bug and doesn't care to fix it.
Is there a way to re-introduce the Ctrl + Break function to VBA so if this happens in the future I don't lose work / force close?
Alt + Esc. Hold down the keys until it breaks.
From Windows 7 on, this will cycle through all open windows. Pay no mind, just keep squeezing it.
On Office 2013, at least, Ctrl+Scroll Lock does it. I didn't need to change any settings or enable anything.
(Blatant plug: my blog post linking to the original source of this info :) )
One way to mitigate this very annoying behaviour is to include
DoEvents
In the loop. It doesn't have to be executed every iteration, so long as it is called periodically, Ctrl Break will still work.
Note: I usually remove this after the code is debugged, to avoid any performance impact
For those who tried the previously proposed solutions to no avail, try this. It worked for me (windows8, Excel 2016)
On the VBA code window, left-click and hold (as though you are going to drag that window) while holding down the Alt+ESC keys. It broke the execution and asked me if I wanted to continue, or debug... I of course chose debug.
If it can help anyone, pressing Ctrl+break when the macro is not running only works if you do it outside of the developer.
An alternative will be introduce error intentionally in the loop. Say for e.g. your loop should not be running more than 1000 times:
Function XYZ()
do while(..)
errcnt = errcnt + 1
if errcnt > 1000 then
cells.find(what:="Chunk Norris").activate
exit function
endif
loop
end function
it will produce an error and 'break' the code
Break (Ctrl + Break) is also not available in the VBA editor if you are in design mode. In that case you can press the Design Mode icon which is to the right of the square stop icon. This brings the editor back in regular mode and the break option then becomes available.
There is no "Break" button on my poor keyboard. My only alternative is to hit the X to close the program or alt+F4. I'll have to code something as well.
From answers.microsoft.com, simply press ESC a few seconds when the focus is on your Excel window. At least, it works for me on Excel 2016.

Resources