Microsoft KB Archive/213745

= XL2000: How to Simulate a Paused Visual Basic Procedure =

Article ID: 213745

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



This article was previously published under Q213745





SUMMARY
There is no built-in method for pausing a macro in Microsoft Excel. This article discusses a method for pausing a macro to allow user input, and then continuing when the user clicks a button.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

To create a macro that pauses for user input and then continues after the user clicks a button, follow these steps:  Close and save any open workbooks, and then open a new workbook. Start the Visual Basic Editor (press ALT+F11). Click Module on the Insert menu.  Type or paste the following procedures into the Code window for Module1: Sub CreatePauseToolbar

Dim NewBar As Object

'Creates the toolbar, and sets a variable to easily format it   Set NewBar = CommandBars.Add

With NewBar

'Sets the toolbar's name .Name = "Pause"

'Makes the toolbar visible .Visible = True

'Adds a button to the toolbar .Controls.Add Type:=msoControlButton

With .Controls(1)

'Sets the style of the button to text only .Style = msoButtonCaption

'Sets the caption of the button .Caption = "Continue"

'Assigns the macro PartTwo to the button .OnAction = "PartTwo"

End With End With End Sub

Sub PartOne

'Prompt the user to select a range MsgBox "Select the Border Range, then" & Chr(13) & _ "click the Continue button."

'Run the procedure that creates the toolbar CreatePauseToolbar

End Sub

Sub PartTwo

'Applies the border to the selected range Selection.BorderAround Weight:=xlThick

'Deletes the Pause toolbar CommandBars("Pause").Delete

End Sub  Click Close and Return to Microsoft Excel on the File menu. On the Tools menu, point to Macro, and then click Macros. In the Macros dialog box, click PartOne, and then click Run.

The PartOne procedure prompts you to select a range of cells.</li> Click OK.

The PartOne procedure calls the CreatePauseToolbar procedure, which creates the Pause toolbar and makes it visible.</li> Select a range of cells on Sheet1.</li> Click the Continue button on the Pause toolbar. Clicking the Continue button calls the PartTwo procedure, which applies an outline border to the current selection, and then deletes the Pause toolbar.</li></ol>

NOTE: In this example, the macro action stops at the end of the PartOne procedure. As a result, all procedure-level variables that have been declared in the PartOne procedure lose their values before the PartTwo procedure is run.

<div class="references_section">