Step-by-step process to show a processing window While Macro Runs in excel:
- Start a new workbook in Microsoft Excel 97, 2003, 2007, 2010, 2013, 2016. (or a new document in Microsoft Word 97, 2003, 2007, 2010, 2013, 2016, 2019, Office365).
- 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 or copy-paste the following code:
- 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.
- 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:
- In the Code window, click Activate event in the Procedure list, and type the following:
-
On the Insert menu, click Module. Type the following code in the
Code window:
- 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.
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
Property Setting
-------------------------------------
Name Processing_Dialog
Caption Please Wait...
StartUpPosition 2-CenterScreen
Private Sub UserForm_Initialize()
lblMessage.Caption = Processing_Message 'Change the Label Caption
End Sub
Private Sub UserForm_Activate()
Me.Repaint 'Refresh the UserForm
Application.Run Macro_to_Process 'Run the macro
Unload Me 'Unload the UserForm
End Sub
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
How to Use the Processing Dialog Box in Other Workbooks
To use this "Processing, Please Wait" dialog box with macros in other workbooks 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.
No comments:
Post a Comment