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

31,475

Solution 1

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

Solution 2

I'm afraid the answer is no, there is no way to do this without inserting code or breakpoints in each point-of-entry procedure.

Why isn't there a way to do this? Well, the real question is, why should there be a way to do this? What's the point? Perpetual debug mode makes no sense. The idea of debug mode is to enter it if and only if you need to debug a specific procedure or error. I'm not sure what you're trying to achieve exactly?

Anyhow, the way you suggest, with callThisOnEveryMethod, is probably the closest you can get. Or actually, no need to define a new procedure: just use

Debug.Assert MY_GLOBAL_DEBUG_BOOLEAN

at the top of your point-of-entry procedures, where MY_GLOBAL_DEBUG_BOOLEAN is a public module-level constant that must be set to False in order to enter debug mode.

Share:
31,475
enderland
Author by

enderland

Farewell Stack Exchange. o7 to everyone I've shared labors with over these many years. Take care.

Updated on November 02, 2020

Comments

  • enderland
    enderland over 3 years

    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?

  • enderland
    enderland over 10 years
    This isn't really what I'm looking for. I can get this exact functionality much easier simply by inserting an intermediate method and adding lots of Stop around my code is even worse than this in terms of maintenance or eventual deployment...
  • Kazimierz Jawor
    Kazimierz Jawor over 10 years
    @enderland- So, use Debug.Assert then to switch between stop (Debug.Assert False) or go (Debug.Assert True) where False and True could be passed as parameters. Or...elaborate more precisely on what you need giving more accurate example.
  • enderland
    enderland over 10 years
    I clarified the question.
  • Kazimierz Jawor
    Kazimierz Jawor over 10 years
    No way to do so simply. Therefore Debug.Assert Condition=False seems to be best option.