Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force VBA editor to enter break mode whenever code is called?

I would like to be able to toggle on/off the option to enter break mode whenever my VBA code is called. The only way I know to do this is set breakpoints at all the "points of entry" into code or have literally every one of these methods call a separate function for purposes of debug.

The "points of entry" may be button clicks or worksheet events and there are a fair number of them.

For example, I could do:

Private Sub bt1Click()
    callThisOnEveryMethod
    'other code
End Sub
Private Sub bt2Click()
    callThisOnEveryMethod
    'other code
End Sub
'etc, repeat 100 times
Private Sub callThisOnEveryMethod()
    'set breakpoint on this method
End Sub

This is not really ideal since I am depending on me adding that to each method and every subsequent method. I don't really trust myself to get 100% of them this way and it's a lot of clutter for debug purposes only. I can add other code here too and even wrap it in an if MY_GLOBAL_DEBUG_BOOLEAN then type statement but I still need to add this code (or the calling method) to every method I write which could start VBA execution.

Imagine I might have 100 methods which could be the start of VBA code executing.

It is not ideal to setup and remove break points on EVERY method each time I want to do this, either, because of the number to turn on/off.

What I would like is to tell VBA somehow "whenever you start executing code, immediately break and go into debug mode regardless of breakpoints, assertions, and without requiring each method to have lots of extra code."

Is this possible?

like image 297
enderland Avatar asked Nov 05 '13 20:11

enderland


1 Answers

There are two options to break code and go into debug mode:

a) with simple Stop instruction:

Sub MyProcedure()
   '...any code here
   Stop     'execution will stop here, debugging will start here
   '...the rest of the code
End sub

b) with Debug.Assert False in this way:

Sub MyProcedure()
   '...any code here
   Debug.Assert False     'execution will stop here, debugging will start here
   '...the rest of the code
End sub

However, you could use any condition working with Debug.Assert Condition- each time condition will return False the code will stop. One sample:

Dim A
A=10
Debug.Assert A<>10   'stopping execution here... entering debugging mode
like image 177
Kazimierz Jawor Avatar answered Sep 26 '22 13:09

Kazimierz Jawor