How do I pause a macro when a condition is met?

John Carney
John Carney used Ask the Experts™
on
I have a macro that performs a series of functions on a multitude of tabs. I don't want to stop the macro, I want it to pause it at a specified sheet so I can step through it, and then if it works properly, resume the macro.

Thanks,
John
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
Use Stop with an IF condition
i.e.

If ws.Name = "Sheet2" Then Stop

Open in new window


Once the above condition is met, the code will pause at Stop and then you can debug the rest of the code.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Stop can be used if meant unconditional. If you want to check a condition, use Debug.Assert «Condition» with «Condition» needing to be False to stop. Background: That command is used in cases where you want to be sure the checked condition is true for further processing, e.g. objects are valid; otherwise stop into the debugger.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Quelmo, correct me if I'm wrong but  I believe that Debug.Assert is only recognized while debugging and that doesn't seem to be what the asker wants.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
The same thing for Stop, and I believe the only way to "pause" a macro is to Exit Sub.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Exit Sub is no pause, it is an exit, leaving the current procedure, without any means to continue execution.
Stop and Debug.Assert are debugging commands, that is correct. But that is what the Asker asked for - a means to execute until a certain code part, than single-step or continue execution. This is debugging, and the debugger is part of VBA.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Exit Sub is no pause
I know and I realize now that I misread the question, so...

gabriel, the easiest way to pause a macro while debugging is to put a breakpoint at the line where you want it to pause. When you run the coded the macro will pause there. You do that by clicking in the left-hand margin of the line. You may also be interested in this article I wrote on debugging.
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
Here is a simple code which iterates over the sheets in the workbook and perform some actions on each sheet.

Objective: Asker wants to debug the code before the code performs those actions on Sheet2.

The following sample codes demonstrate the use of Stop, Debug.Assert and Breakpoint for the same scenario.

Sub UsingStop()
Dim ws As Worksheet
For Each ws In Worksheets
    ws.Activate
    'With Stop
    'To pausse the code, condition being checked should be true
    'So if you want to pause the code when the code is working on Sheet2
    'you will check the condition ws.Name = "Sheet2" with Stop
    If ws.Name = "Sheet2" Then Stop
    
    'Code performs some action on the worksheet here
    With ws
        'blah
        'blah
        'blah
    End With
Next ws
End Sub

Open in new window


 Sub UsingDebugAssert()
Dim ws As Worksheet
For Each ws In Worksheets
    ws.Activate
    'With Debug.Assert
    'To pausse the code, condition being checked should be false
    'So if you want to pause the code when the code is working on Sheet2
    'you will check the condition ws.Name <> "Sheet2" with debug.assert
    Debug.Assert ws.Name <> "Sheet2"
    
    'Code performs some action on the worksheet here
    With ws
        'blah
        'blah
        'blah
    End With
Next ws
End Sub

Open in new window


Sub UsingBreakpoint()
Dim ws As Worksheet
For Each ws In Worksheets
    If ws.Name = "Sheet2" Then
        'You will insert a Breakpoint at line 6 to pause the code
        ws.Activate
        'Code performs some action on the worksheet here
        With ws
            'blah
            'blah
            'blah
        End With
    End If
Next ws
End Sub

Open in new window


So any method can be used based on the requirement and I normally prefer Stop and Breakpoint.
Roy CoxGroup Finance Manager

Commented:
Exit Sun will stop the macro

End will also do the same but will clear any Global variables.
Older than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
The problem with using Stop is that you would have to remember to remove that line(s) of code before the workbook went into production or you might get a surprise months down the road (depending on the rarity of the condition occurring). I think it  would be better to go to Debug-> Add Watch and create a watch that looks like this which would stop when the condition was True.
Add Watch

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial