6

Say I have a big primary routine, that does many things and calls various other programs.

When debugging, I step through my main program with F8 so I can see line by line what's happening.

However, when I do this, any routines I call from that main sub also get stepped through. Instead of scrolling to that called routine's End Sub, and placing a Break, is there a way to have VBA just run that routine, but then stop when I get back to my main program?

Currently, I have two ways to do this. Add a break in the line after I call a sub, in the MAIN routine - or - I add a break in that called routine's End Sub line.

For example,

Sub main_program()
Dim rng As Range
Set rng = Range("A1:Z1000")

do_things rng

Dim cel As Range
For Each cel In rng.SpecialCells(xlCellTypeBlanks)
    Debug.Print cel.Offset(1, 0).Value
Next cel
End Sub

Sub do_things(rng As Range)
Dim cel As Range
For Each cel In rng
    If cel.Value <> "something" Then
        cel.Font.Bold = True
    End If
Next cel
End Sub

So when going through my main program line by line, I'd like to be able to run do_things without having to put a break on For Each cel in rng..., or the End Sub.

I'd guess one solution is to have VBA just automatically break when it hits an End Sub line, or something. I'm open to ideas, I have searched around and can't find anything really other than the current solutions I mentioned above.

(Sorry if the title is a little clunky, I couldn't really think of a better way to phrase it - feel free to edit).

9

There is - although it may not be obvious at first glance.

When you're in that subroutine (although it works on any routine), go to Debug --> Step Out (CTRL+SHIFT+F8).

This will actually complete that routine, then go to the next line in the other code, if applicable. (At first read, I assumed it simply stopped that routine at the current line, and would back out to the routine that called it).

For example:

Sub main_program()
Dim rng As Range
Set rng = Range("A1:B10")

do_things rng

Dim cel As Range
For Each cel In rng.SpecialCells(xlCellTypeBlanks)
    Debug.Print cel.Offset(1, 0).Value
Next cel
End Sub

Sub do_things(rng As Range)
Dim cel As Range
For Each cel In rng
    cel.Value = "Test"
Next cel
For Each cel In rng
    If cel.Row Mod 2 = 1 Then cel.EntireRow.Font.Bold = True
Next cel
End Sub

.gif of the above:

enter image description here

Edit: An excellent comment I wanted to add to make this answer more complete. Per @MathieuGuindon, when the do_things rng line is highlighted, you can do Debug --> Step Over (SHIFT+F8) and run do_things completely, and it'll stop at that next line.

  • 6
    Note: Shift+F8 / "Step Over" when do_things rng is highlighted, would also run the procedure without stepping into it. – Mathieu Guindon Nov 4 '19 at 20:15
  • @MathieuGuindon - Oh! Excellent to know (I added in to my Answer). I have seen that, but admittedly never tried it. Again, just by reading it, I would assume with do_things rng highlighted, and clicking "Step Over" would literally skip that line. Not run it and pause on the next one. Thanks again :D – BruceWayne Nov 4 '19 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.