progress bar for marco


Posted by Paul on September 26, 2001 4:34 PM

What is the code to show a progress bar when a Marco is running? Thanks



Posted by Qroozn on September 26, 2001 5:22 PM

Follow the instructions and code below - e-mail me if you have troubles, and tell me how it went.


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:
Property Value
--------------------------------------------
Caption Now updating. Please wait...

Draw a Frame control on the UserForm


Change the following properties of the Frame control to the following values:
Property Value
-----------------------------
Name FrameProgress

Draw a Label control on the Frame control.


Change the following properties of the Label control to the following values:
Property Value
-------------------------------------
Name LabelProgress
BackColor &H000000FF&
SpecialEffect fmSpecialEffectRaised

Type the Macro Code
Double-click the user form to open the Code window for the user form.


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

On the Insert Menu, click Module.


In the Code window for the module, type the following code:
Sub ShowUserForm()
UserForm1.Show
End Sub

Sub <YOURMACRO>()
' 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