Set breakpoint in VBA code programmatically

16,064

Solution 1

Use the keyword STOP to break te code if a certain condition is true.

Solution 2

There are Two Ways to do that:

  1. Use Stop Key word. Example as given below, set a break point at Stop

     if (x = 21 ) Then
       Stop
      End If
    
  2. Using Add Watch enter image description here

    Go to Debug -> Select Add Watch

    Go to Debug -> Select Add Watch

Solution 3

NB:I know this is an old topic but this could help others.

You could use Watches:

Right click on the variables you wish to monitor -> Add Watch... In Watch Type: 'Break when value changes'

While you run your code, you can check the status of your Watches thanks to the Watch Window (accessible from the 'View' menu)

Solution 4

in the hope someone can benefit from this : In such situations regardless of the programming language used - writing a few lines of code either in Perl, AWK or even shell scripts can solve the problem : search for a regular expression containing the array name (ignoring case). Once you export all modules and classes in the Workbook(s) into a given directory - the scripts can search those for you.

Share:
16,064

Related videos on Youtube

SMir
Author by

SMir

Updated on June 04, 2022

Comments

  • SMir
    SMir about 2 years

    I have a very large piece of code written in VBA (>50,000 lines - numerous modules). There is one array of interest to me, and I'd like to find all the conditions under which the value of any element of this array changes. The values can change in any module. Running the script line by line is not the most efficient option due to the size of the code.

    I am looking for better ways to solve this problem. Two ways that come to my mind is to programmatically set a breakpoint (which I am not sure if can be done) or programmatically insert an if-block after each assignment that somehow alerts me that the value has changed. (not preferred).

    So my question boils down to:

    1. Is it possible to programmatically set breakpoints in VBA code?
    2. If the answer to the above question is No, what is an efficient way to solve this problem?

    UPDATE: Thanks for the comments/replies. As I had implied, I am interested in the least amount of modification to the current code (i.e. inserting if-blocks, etc) and most interested in the break-point idea. I'd like to know if it's doable.

  • Tim Williams
    Tim Williams almost 12 years
    If you use Stop then all state is lost - probably not what the OP is looking for.
  • Alex K.
    Alex K. almost 12 years
    IIRC stop preserves state, ditto for Debug.Assert IfFalse