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

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
LVL 1
John CarneyReliability Business Tools Analyst IIAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
0
Martin LissOlder than dirtCommented:
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.
0
Determine the Perfect Price for Your IT Services

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

Martin LissOlder than dirtCommented:
The same thing for Stop, and I believe the only way to "pause" a macro is to Exit Sub.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
0
Martin LissOlder than dirtCommented:
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.
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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.
0
Roy CoxGroup Finance ManagerCommented:
Exit Sun will stop the macro

End will also do the same but will clear any Global variables.
0
Martin LissOlder than dirtCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.