Adding event-handler procedures. Microsoft Excel VBA Programming
Reklama
Reklama
Chapter 16: UserForm Basics
263
If you’d like a sneak preview to see what the UserForm looks like when it’s displayed, press F5. None of the controls are working yet, so you need to click the red “X” in the title bar to close the dialog box.
Figure 16-5:
This is the
UserForm after adding three Option
Button controls inside a
Frame control.
The Accelerator property determines which letter in the caption is underlined — more important, it determines what Alt-key combination selects that control. For example, you can select the Lower Case option by pressing Alt+L because the L is underlined. Accelerator keys are optional, but some users prefer to use the keyboard to navigate dialog boxes.
You may wonder why the OptionButtons have accelerator keys but the
CommandButtons go without. Generally, OK and Cancel buttons never have accelerator keys because they can be accessed from the keyboard. Pressing
Enter is equivalent to clicking OK because the control’s Default property is True. Pressing Esc is equivalent to clicking Cancel because the control’s
Cancel property is True.
Adding event-handler procedures
Now it’s time to make the UserForm actually do something. Here’s how to add an event-handler procedure for the Cancel and OK buttons:
1. Double-click the Cancel button.
VBE activates the Code window for the UserForm and inserts an empty procedure:
Private Sub CancelButton_Click()
The procedure named CancelButton_Click is executed when the Cancel button is clicked, but only when the dialog box is displayed. In other words, clicking the Cancel button when you’re designing the dialog box
264
Part IV: Communicating with Your Users
won’t execute the procedure. Because the Cancel button’s Cancel property is set to True, pressing Esc also triggers the CancelButton_Click procedure.
2. Insert the following statement inside the procedure (before the End
Sub statement):
Unload UserForm1
This statement closes the UserForm (and removes it from memory) when the Cancel button is clicked.
3. Press Shift+F7 to return to the UserForm.
4. Double-click the OK button.
VBE activates the code window for the UserForm and inserts an empty
Sub procedure called
Private Sub OKButton_Click()
When the UserForm is displayed, clicking OK executes this procedure.
Because this button has its Default property set to True, pressing Enter also executes the OKButton_Click procedure.
5. Enter the following code inside the procedure:
Private Sub OKButton_Click()
Dim WorkRange As Range
‘ Process only text cells, no formulas
On Error Resume Next
Set WorkRange = Selection.SpecialCells _
(xlCellTypeConstants, xlCellTypeConstants)
‘ Upper case
If OptionUpper Then
For Each cell In WorkRange
cell.Value = UCase(cell.Value)
Next cell
End If
‘ Lower case
If OptionLower Then
For Each cell In WorkRange
cell.Value = LCase(cell.Value)
Next cell
End If
‘ Proper case
If OptionProper Then
For Each cell In WorkRange
cell.Value = Application. _
WorksheetFunction.Proper(cell.Value)
Stiahnuť
Reklama