This article was previously published under Q162257
This article has been archived. It is offered "as is" and will no longer be updated.
This article describes how you can display a dialog box while a macro isrunning in the background. The purpose of displaying a dialog box is toalert the user that the application is busy while your macro is running.
When you use the Show method to display a UserForm, the UserForm is shown"modal"; that is, the user must respond to the UserForm before using anyother part of the application. After a UserForm is loaded, no subsequentcode is executed until the UserForm is either hidden or unloaded.
To display a dialog box and allow the macro to run in the background, youcan run your macro in the Activate event of the UserForm. When the UserFormis shown, the Activate event is triggered and your macro will run while theUserForm is shown on the screen. The following steps illustrate how you canaccomplish this:
Microsoft provides examples of Visual Basic for Applications procedures forillustration only, without warranty either expressed or implied, including,but not limited to the implied warranties of merchantability and/or fitnessfor a particular purpose. The Visual Basic procedures in this article areprovided 'as is' and Microsoft does not guarantee that they can be used inall situations. While Microsoft support professionals can help explain thefunctionality of a particular macro, they will not modify these examples toprovide added functionality, nor will they help you construct macros tomeet your specific needs. If you have limited programming experience, youmay want to consult one of the Microsoft Solution Providers. SolutionProviders offer a wide range of fee-based services, including creatingcustom macros. For more information about Microsoft Solution Providers,call Microsoft Customer Information Service at (800) 426-9400.
Start a new workbook in Microsoft Excel 97 (or a new document in Microsoft Word 97).
Press ALT+F11 to activate the Visual Basic Editor.
On the Insert menu, click Module to insert a module into the project.
Press F4 to display the Properties window.
Change the Name property of the module to Processing_Code.
In the Processing_Code module window, type the following:
Public Processing_Message As String Public Macro_to_Process As String Sub StartProcessing (msg As String, code As String) Processing_Message = msg 'Set the message that is displayed 'in the dialog box Macro_to_Process = code 'Set the macro that is run after the 'dialog box is active Processing_Dialog.Show 'Show the Dialog box End Sub
On the Insert menu, click UserForm to add a UserForm to the project.
Press F4 to display the Properties Window. Change the UserForm properties to the following settings.
Property Setting ------------------------------------- Name Processing_Dialog Caption (Leave Blank) StartUpPosition 2-CenterScreen
Add one Label control to the UserForm. Change the Name property of the label to lblMessage.
Select the UserForm. On the View menu, click Code. In the code window, in the Procedure list, select the Initialize event, and then type the following in the Code window:
Private Sub UserForm_Initialize() lblMessage.Caption = Processing_Message 'Change the Label 'Caption End Sub
In the Code window, click Activate event in the Procedure list, and type the following:
Private Sub UserForm_Activate() Me.Repaint 'Refresh the UserForm Application.Run Macro_to_Process 'Run the macro Unload Me 'Unload the UserForm End Sub
On the Insert menu, click Module. Type the following code in the Code window:
Sub MyMacro() For x = 1 to 5000 Application.StatusBar = x '5000 Iterations Changing 'StatusBar Next Application.StatusBar = False 'Reset the StatusBar End Sub Sub Main() 'Call the StartProcessing procedure to show the Processing_Dialog 'with the label "Processing, Please Wait..." and execute 'MyMacro. StartProcessing "Processing, Please Wait...", "MyMacro" End Sub
Press ALT+Q to leave the Visual Basic Editor and return to Microsoft Excel (or Microsoft Word).
On the Tools menu, point to Macro, and click Macros. Select the Main macro and click Run. The Processing_Dialog dialog box appears.
While the dialog box is shown, the status bar text in the applicationincrements from 1 to 5000--the changing status bar text is the indicationthat your macro is running while the dialog box is on the screen.
How to Use the Processing Dialog Box in Other Workbooks
To use this "Processing, Please Wait" dialog box with macros in otherworkbooks or documents, do the following:
Activate the Visual Basic Editor. On the View menu, click Project Explorer.
Select the project that you created in the previous steps.
Right-click the "Processing_Dialog" UserForm and click Export File. Save the UserForm as "Processing_Dialog.frm."
Right-click the "Processing_Code" Module and click Export File. Save the module as "Processing_Code.bas."
Return to Microsoft Excel and open the workbook in which you would like to use the "Processing, Please Wait" dialog box.
Activate the Visual Basic Editor.
On the File menu, click Import File, select the "Processing_Dialog.frm" file, and click Open.
On the File menu, click Import File. Select the "Processing_Dialog.bas" file, and click Open.
On the Insert menu, click Module, and type the following code in the Code window:
Sub Main() StartProcessing "<message text>", "<macro name>" End Sub
Where the <message text> argument is the text string that you want to display in the Processing_Dialog dialog box and <macro name> is the macro that you would like to run after the dialog box appears. To show the dialog box and run the macro indicated by "<macro name>", run the macro Main.
For information on how to show a temporary message box while a macro isrunning in Microsoft Excel versions 5.x or 7.x, please see the followingarticle in the Microsoft Knowledge Base:
148209 XL: How to Create a Temporary Message Box While Macro Runs