Friday, April 15, 2016

Show a Processing Window While Macro Runs

Step-by-step process to show a processing window While Macro Runs in excel:



Processing GIF Image

  1. 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).
  2. Press ALT+ F11 to activate the Visual Basic Editor.
  3. On the Insert menu, click Module to insert a module into the project.
  4. Press F4 to display the Properties window.
  5. Change the Name property of the module to Processing_Code.
  6. In the Processing_Code module window, type or copy-paste the following code:
  7. 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
    

  8. On the Insert menu, click UserForm to add a UserForm to the project.
  9. Press F4 to display the Properties Window. Change the UserForm properties to the following settings.




  10. Property           Setting
    -------------------------------------
    Name               Processing_Dialog
    Caption            Please Wait...
    StartUpPosition    2-CenterScreen
    
  11. Add one Label control to the UserForm. Change the Name property of the label to lblMessage.
  12. 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:
  13. Private Sub UserForm_Initialize()
        lblMessage.Caption = Processing_Message  'Change the Label Caption
    End Sub
    
  14. In the Code window, click Activate event in the Procedure list, and type the following:
  15. Private Sub UserForm_Activate()
          Me.Repaint                         'Refresh the UserForm
          Application.Run Macro_to_Process   'Run the macro
          Unload Me                          'Unload the UserForm
    End Sub
    

  16. On the Insert menu, click Module. Type the following code in the Code window:




  17. 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
    
  18. Press ALT+ Q to leave the Visual Basic Editor and return to Microsoft Excel (or Microsoft Word).
  19. 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 application increments from 1 to 5000--the changing status bar text is the indication that 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 other workbooks or documents, do the following:
  1. Activate the Visual Basic Editor. On the View menu, click Project Explorer.
  2. Select the project that you created in the previous steps.
  3. Right-click the "Processing_Dialog" UserForm and click Export File. Save the UserForm as "Processing_Dialog.frm."
  4. Right-click the "Processing_Code" Module and click Export File. Save the module as "Processing_Code.bas."
  5. Return to Microsoft Excel and open the workbook in which you would like to use the "Processing, Please Wait" dialog box.
  6. Activate the Visual Basic Editor.
  7. On the File menu, click Import File, select the "Processing_Dialog.frm" file, and click Open.
  8. On the File menu, click Import File. Select the "Processing_Dialog.bas" file, and click Open.
  9. On the Insert menu, click Module, and type the following code in the Code window:
  10. 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