在Excel VBA中:
我正在创建一个表单。这个表单有几个单选按钮组,其中一些有很多选项(但每组只能有一个单选按钮)。我希望能够得到那些" s" true"的无线电按钮的名称。每组,而不是必须检查每个单选按钮的状况。
例如:
FAMILY A
FAMILY B
我必须做什么:
我想做什么:
这可能吗?
感谢您的期待!
修改 解!基于David的建议如下:
Dim ctrl As MSForms.Control
Dim dict(5, 1)
Dim i
'## Iterate the controls, and associates the GroupName to the Button.Name that's true.
For Each ctrl In Me.Controls
If TypeName(ctrl) = "OptionButton" Then
If ctrl.Value = True Then
dict(i, 0) = ctrl.GroupName
dict(i, 1) = ctrl.Name
i = i + 1
End If
End If
答案 0 :(得分:2)
这样的事似乎有效。我把它放在一个CommandButton点击事件处理程序中,但你可以把它放在任何地方。
Sub CommandButton1_Click()
Dim ctrl As MSForms.Control
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
'## Iterate the controls, and add the GroupName and Button.Name
' to a Dictionary object if the button is True.
' use the GroupName as the unique identifier/key, and control name as the value
For Each ctrl In Me.Controls
If TypeName(ctrl) = "OptionButton" And ctrl.Value = True Then
dict(ctrl.GroupName) = ctrl.Name
End If
Next
'## Now, to call on the values you simply refer to the dictionary by the GroupName, so:
Debug.Print dict("Family A")
Debug.Print dict("Family B")
Set dict = Nothing
End Sub