Private Sub UserForm_Initialize()
Dim ButtonCount As Integer
Dim Ctl As Control
ButtonCount = 0
For Each Ctl In Me.Controls
If TypeName(Ctl) = "CommandButton" Then
If Ctl.Name <> "OKButton" Then
ButtonCount = ButtonCount + 1
ReDim Preserve Buttons(1 To ButtonCount)
Set Buttons(ButtonCount).ButtonGroup = Ctl
End If
End If
Next
Stop
End Sub
类模块:
Public WithEvents ButtonGroup As MSForms.CommandButton
Private Sub ButtonGroup_Click()
Dim Msg As String
Msg = "你单击了" & ButtonGroup.Name & Chr(13)
Msg = Msg & "按钮是:" & ButtonGroup.Caption & Chr(13)
MsgBox Msg, vbInformation, ButtonGroup.Name
End Sub