Microsoft Office Tutorials and References
In Depth Information
Handling Multiple UserForm Controls with One Event Handler
This procedure is kicked off by the UserForm’s Initialize event. Notice that the code
excludes a button named OKButton from the button group. Therefore, clicking the OK
button doesn’t execute the ButtonGroup_Click procedure.
Dim Buttons() As New BtnClass
Private Sub UserForm_Initialize()
Dim ButtonCount As Integer
Dim ctl As Control
‘ Create the Button objects
ButtonCount = 0
For Each ctl In UserForm1.Controls
If TypeName(ctl) = “CommandButton” Then
‘Skip the OKButton
If ctl.Name <> “OKButton” Then
ButtonCount = ButtonCount + 1
ReDim Preserve Buttons(1 To ButtonCount)
Set Buttons(ButtonCount).ButtonGroup = ctl
After performing these steps, you can execute the ShowDialog procedure to display the
UserForm. Clicking any of the CommandButtons (except the OK button) executes the
ButtonGroup_Click procedure. Figure 15-22 shows an example of the message displayed
when a button is clicked.
Figure 15-22: The ButtonGroup_Click procedure describes the button that was clicked.