Microsoft Office Tutorials and References
In Depth Information
Animating a Label
StartStopButton.Caption = “Start”
End If
End Sub
Because the button serves two purposes (starting and stopping), the procedure uses a public
variable, Stopped , to keep track of the state. The first part of the procedure consists of two
If-Then structures to validate the contents of the TextBox controls. Two more statements
ensure that the low value is in fact less than the high value. The next section adjusts the Label
control’s font size, based on the maximum value. The Do Until loop is responsible for
generating and displaying the random numbers. Notice the DoEvents statement. This statement causes
Excel to “yield” to the operating system. Without the statement, the Label control wouldn’t
display each random number as it’s generated. In other words, the DoEvents statement is what
makes the animation possible.
The UserForm also contains a CommandButton that serves as a Cancel button. This control is
positioned off the UserForm so that it’s not visible. This CommandButton has its Cancel
property set to True , so pressing Esc is equivalent to clicking the button. Its click event-handler
procedure simply sets the Stopped variable to True and unloads the UserForm:
Private Sub CancelButton_Click()
Stopped = True
Unload Me
End Sub
This example, named random number generator.xlsm , is available on the
companion CD-ROM.
Search JabSto ::

Custom Search