Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a VBA command that causes a debug to start at that point?

Tags:

excel

vba

The context of this question is that I am using message boxes as a tool to help me get familiar with a quite large amount of VBA macros that are running on a collection of Excel workbooks.

I am carefully inserting message boxes within the code which pop up and tell/remind me where we are in the code. I would like to have a button in these boxes which will take me into a debug of the code at that point.

At the moment my solution is to perform a division-by-zero if the `Yes' button is chosen. Here is an example snippet:

Dim MyError as Double
...
If MsgBox("Just entered function XYZ(). Want to debug?", vbYesNo + vbDefaultButton2) = vbYes Then MyError = 1# / 0

It works, but is not very elegant.

I was hoping there is a command which will start the VBA debug mode at the point that command is called.

like image 633
Robert Avatar asked Feb 20 '13 10:02

Robert


People also ask

How do I run VBA in Debug mode?

One of the methods used to debug VBA code is by running the code. The shortcut key for the command is F5. Start by placing the cursor into the UserForm or Sub (macro) and then press F5 to run the sub. Please note that F5 will not work when running a sub that requires parameters to execute a function.

Which key is used to Debug or step through the macro?

F8 Key Stops Working. When debugging Excel VBA code, you can use the F8 key, or the Step Into command, to step through the code, one line at a time.

Can you Debug a function in VBA?

The best way to debug a user defined function (UDF) is by writing a temporary Sub procedure that calls your function and then step into the Sub procedure by pressing F8 key.


2 Answers

The Stop command does this for you:

If MsgBox("Just entered function XYZ(). Want to debug?", vbYesNo + vbDefaultButton2) = vbYes Then Stop
like image 129
Peter Albert Avatar answered Sep 19 '22 15:09

Peter Albert


Yes, there is:

Debug.Assert False
like image 24
Dirk Vollmar Avatar answered Sep 18 '22 15:09

Dirk Vollmar