30.04.2017 Views

4523756273

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 18: UserForm Techniques and Tricks<br />

307<br />

The procedures<br />

This example uses two procedures and a module level variable.<br />

✓ The module level variable. Located in a VBA module. This variable<br />

holds the copy of the userform:<br />

Dim ProgressIndicator as UserForm1<br />

✓ EnterRandomNumbers. It does all the work and is executed when the<br />

UserForm is shown. Notice that it calls the UpdateProgress procedure,<br />

which updates the progress indicator in the dialog box:<br />

Sub EnterRandomNumbers ()<br />

‘ Inserts random numbers on the active worksheet<br />

Dim Counter As Long<br />

Dim RowMax As Long, ColMax As Long<br />

Dim r As Long, c As Long<br />

Dim PctDone As Single<br />

‘ Create a copy of the form in a variable<br />

Set ProgressIndicator = New UserForm1<br />

‘ Show ProgressIndicator in modeless state<br />

ProgressIndicator.Show vbModeless<br />

If TypeName(ActiveSheet) “Worksheet” Then<br />

Unload ProgressIndicator<br />

Exit Sub<br />

End If<br />

‘ Enter the random numbers<br />

Cells.Clear<br />

Counter = 1<br />

RowMax = 200<br />

ColMax = 50<br />

For r = 1 To RowMax<br />

For c = 1 To ColMax<br />

Cells(r, c) = Int(Rnd * 1000)<br />

Counter = Counter + 1<br />

Next c<br />

PctDone = Counter / (RowMax * ColMax)<br />

Call UpdateProgress(PctDone)<br />

Next r<br />

Unload ProgressIndicator<br />

Set ProgressIndicator = Nothing<br />

End Sub<br />

✓ UpdateProgress. This procedure accepts one argument and updates the<br />

progress indicator in the dialog box:<br />

Sub UpdateProgress(pct)<br />

With ProgressIndicator<br />

.FrameProgress.Caption = Format(pct, “0%”)<br />

.LabelProgress.Width = pct * (.FrameProgress _

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!