Microsoft KB Archive/170782

= XL97: How to Display a Progress Bar with a User Form =

Article ID: 170782

Article Last Modified on 6/12/2007

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q170782



For a Microsoft Excel 2000 and 2002 version of this article, see 211736.

Article idea submitted by: John Walkenbach



SUMMARY
If you have a Microsoft Visual Basic for Applications macro that takes a long time to complete, you may want to give the user an indication that the macro is progressing normally. This article shows you how to create a progress bar with a user form.



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

Create the User Form
In the following example, a Visual Basic subroutine populates a large range of cells with a random number. This task takes several seconds to complete; the indicator shows you that the macro is running normally.  Close and save any open workbooks, and then open a new workbook. Start the Visual Basic Editor (press ALT+F11). If the Properties window is not visible, click Properties on the View menu (or press F4). On the Insert menu, click UserForm. Draw a Label control on the UserForm.  Change the following properties of the Label control to the following values: <pre class="fixed_text">      Property        Value Caption        Now updating. Please wait... </li> Draw a Frame control on the UserForm</li>  Change the following properties of the Frame control to the following values: <pre class="fixed_text">      Property        Value -      Name            FrameProgress </li> Draw a Label control on the Frame control.</li>  Change the following properties of the Label control to the following values: <pre class="fixed_text">      Property        Value -      Name            LabelProgress BackColor      &H000000FF& SpecialEffect  fmSpecialEffectRaised </li></ol>

Type the Macro Code
<ol> Double-click the user form to open the Code window for the user form.</li>  In the module, type the following code for the UserForm_Activate event: Private Sub UserForm_Activate ' Set the width of the progress bar to 0. UserForm1.LabelProgress.Width = 0

' Call the main subroutine. Call Main End Sub </li> On the Insert Menu, click Module.</li>  In the Code window for the module, type the following code: Sub ShowUserForm UserForm1.Show End Sub

Sub Main Dim Counter As Integer Dim RowMax As Integer, ColMax As Integer Dim r As Integer, c As Integer Dim PctDone As Single

Application.ScreenUpdating = False ' Initialize variables. Counter = 1 RowMax = 100 ColMax = 25

' Loop through cells. For r = 1 To RowMax For c = 1 To ColMax 'Put a random number in a cell Cells(r, c) = Int(Rnd * 1000) Counter = Counter + 1 Next c

' Update the percentage completed. PctDone = Counter / (RowMax * ColMax)

' Call subroutine that updates the progress bar. UpdateProgressBar PctDone Next r         ' The task is finished, so unload the UserForm. Unload UserForm1 End Sub

Sub UpdateProgressBar(PctDone As Single) With UserForm1

' Update the Caption property of the Frame control. .FrameProgress.Caption = Format(PctDone, "0%")

' Widen the Label control. .LabelProgress.Width = PctDone * _ (.FrameProgress.Width - 10) End With

' The DoEvents allows the UserForm to update. DoEvents End Sub </li> Return to Microsoft Excel (press ALT+F11).</li> Run the ShowUserForm subroutine.</li></ol>

A dialog box is displayed with a red progress bar that increases in size as the Main subroutine populates the cells on the worksheet.

The ShowUserForm subroutine shows the user form. The procedure attached to the Activate event of the user form calls the Main subroutine. The Main subroutine, in addition to its main task (populating cells with random numbers), calls the UpdateProgressBar subroutine, which updates the Label control on the UserForm.

NOTE: Using this technique, your macro takes just a bit longer to complete its intended tasks.

<div class="references_section">