XL98: How to Simulate a Paused Visual Basic Procedure (183850)



The information in this article applies to:

  • Microsoft Excel 98 Macintosh Edition

This article was previously published under Q183850

SUMMARY

There is no built-in method for pausing a Visual Basic for Applications macro in Microsoft Excel 98 Macintosh Edition. This article contains one method for stopping a macro for 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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. To use this method, follow these steps:
  1. Close and save any open workbooks, and then open a new workbook.
  2. Start the Visual Basic Editor (press OPTION+F11).
  3. On the Insert menu, click module.
  4. Type the following procedures into the code window for Module1.
          Sub CreatePauseToolbar()
    
              Dim NewBar As CommandBar
    
              ' 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
    						
  5. Click "Close and Return to Microsoft Excel" on the File menu.
  6. Point to Macros on the Tools menu, 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.
  7. Click OK.

    The PartOne procedure calls the CreatePauseToolbar procedure, which creates the Pause toolbar and makes it visible.
  8. Select a range of cells on Sheet1.
  9. 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.

NOTE: In this example, macro execution actually ends 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 executed.

REFERENCES

For additional information about the lifetime of variables, please see the following article here in the Microsoft Knowledge Base:

141693 XL: Scope of Variables in Visual Basic for Applications

For more information about command bars, from the Visual Basic Editor, click the Office Assistant, type commandbars, click Search, and then click to view "Using command bars."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:

176476 OFF: Office Assistant Not Answering Visual Basic Questions


Modification Type:MajorLast Reviewed:6/17/2005
Keywords:kbdtacode kbhowto kbProgramming KB183850