【问题标题】:vba get true radio button val from groupvba 从组中获取真正的单选按钮值
【发布时间】:2014-05-11 00:20:47
【问题描述】:

在 Excel VBA 中:

我正在创建一个表单。这个表单有几个单选按钮组,其中一些有很多选项(但每个组只能有一个单选按钮)。我希望能够获得每个组为“true”的单选按钮的名称,而不必检查每个单选按钮的条件。

例如:

一家人

  • 选项 1 - F
  • 选项 2 - T

家庭成员

  • 选项 11 - F
  • 选项 12 - F
  • 选项 13 - F
  • 选项 14 - F
  • 选项 15 - T

我必须做什么

  • 选项 1 是真的吗?
  • 选项 2 是真的吗? (是的......所以家庭 A 的选项 2)
  • 选项 11 是真的吗?
  • Option12 是真的吗?
  • 选项 13 是真的吗?
  • Option14 是真的吗?
  • 选项 15 是真的吗? (是的......所以家庭 B 的选项 15)

我想做的事

  • 什么按钮适用于家庭 A? (选项2)
  • 什么按钮适用于家庭 B? (选项 15)

这可能吗?

感谢收看!

编辑: 解决方案!根据大卫的以下建议:

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

【问题讨论】:

  • 那么,您将如何定义组/家庭?按钮在框架内吗?你怎么知道一个小组什么时候开始和结束?如果没有某种迭代或没有额外的变量来始终存储true 按钮名称,这是不可能实现的。
  • 组/族名是一个名为 GroupName 的属性。默认情况下,它将 all 设置为同一个组,但您可以更改该属性以创建新的族。所以每个单选按钮都知道它属于哪个组,因为它保存在 GroupName 属性中。
  • 框架内的按钮似乎比 GroupName 属性更可靠很多。在任何情况下,当找到True 时,您肯定必须使用Exit For 语句在For...Next 循环中迭代每个组。这并不是特别复杂。我会看看我能不能想象出你的设置可能会更容易一些。
  • 我在想一个类(List)来存储所有真实的,因为家庭只能有一个真实的,你只需要存储家庭,它是当前的真实价值。这只需要与列表长 = 家庭数量一样多的迭代。

标签: vba radio-group userform radio-button


【解决方案1】:

这样的事情似乎有效。我已经把它放在一个 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

【讨论】:

  • 是的,先生!你让我足够接近。我用了你的脚本,修改了一些东西。我没有创建字典对象,而是将所有内容存储在一个数组中(因为组名有点随机)。这样我就可以查询组名,得到对应的TRUE最性感!再次感谢。
【解决方案2】:

如果您不想构建字典,这将获得组的选定选项按钮。 Excel 2010 中似乎不存在已接受答案中的属性 .Controls。

Function GetSelectedRadioButton(oSheet As Worksheet, groupName As String) As MSForms.optionButton
    Dim OleObj As OLEObject

    For Each OleObj In oSheet.OLEObjects
        If OleObj.progID = "Forms.OptionButton.1" Then
            If OleObj.Object.groupName = groupName Then
                If OleObj.Object.Value = True Then
                    Set GetSelectedRadioButton = OleObj.Object
                    Exit Function
                End If
            End If
        End If
    Next OleObj

    Set GetSelectedRadioButton = Nothing
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 2011-03-14
    • 2011-03-28
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2018-10-07
    相关资源
    最近更新 更多