vba 从 Excel 用户表单中的组中返回选定的单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41442863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:48:10  来源:igfitidea点击:

Return selected radiobutton from group in Excel userform

excelvbaradio-buttonuserformgroupname

提问by PeteBradshaw

I have an Excel userform that has a number of radio (option) buttons grouped together.

我有一个 Excel 用户表单,其中包含许多组合在一起的单选(选项)按钮。

Is it possible to reference the GroupName of the radio buttons to identify which one has been selected?

是否可以引用单选按钮的 GroupName 来识别已选择哪个?

I've tried me.myGroup, but Excel doesn't recognise it.

我试过了me.myGroup,但 Excel 无法识别。

If possible, I would like to write something like;

如果可能的话,我想写一些类似的东西;

myVar = me.mygroup

Is this possible in Excel 2013?

这在 Excel 2013 中可行吗?

回答by Robin Mackenzie

If you have set the GroupNameproperty on the option buttons like this:

如果您GroupName在选项按钮上设置了这样的属性:

enter image description here

在此处输入图片说明

Then you can refer to that property in a loop of the controls where you are looking to see that the control's TypeNameis OptionButtonand that the GroupNameis a match:

然后,您可以在要查看控件的TypeNameisOptionButtonGroupNameis 匹配的控件循环中引用该属性:

Option Explicit

Private Sub CommandButton2_Click()
    Dim opt As MSforms.OptionButton

    Set opt = GetSelectedOptionByGroupName("MyGroup")

    If Not opt Is Nothing Then
        MsgBox opt.Name
    Else
        MsgBox "No option selected"
    End If

End Sub

Function GetSelectedOptionByGroupName(strGroupName As String) As MSforms.OptionButton

    Dim ctrl As Control
    Dim opt As MSforms.OptionButton

    'initialise
    Set ctrl = Nothing
    Set GetSelectedOptionByGroupName = Nothing

    'loop controls looking for option button that is
    'both true and part of input GroupName
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "OptionButton" Then
            If ctrl.GroupName = strGroupName Then 
                Set opt = ctrl
                If opt.Value Then
                    Set GetSelectedOptionByGroupName = opt
                    Exit For
                End If
            End If
        End If
    Next ctrl

End Function

回答by Tetra

Morning Pete,

早上好,皮特,

You would need to assign a particular value to your variable in order to determine which button has been clicked.

您需要为变量分配一个特定的值,以确定单击了哪个按钮。

Try something like

尝试类似的东西

Private Sub OptionButton1_Click()

myVar = 1

End Sub

to use a specific value. You can access this subroutine automatically by double-clicking your radio button in the userform editor. This way, later in your code you can reference myVar to determine which action your script should take next, e.g.

使用特定值。您可以通过双击用户表单编辑器中的单选按钮自动访问此子例程。这样,稍后在您的代码中,您可以引用 myVar 来确定您的脚本接下来应该采取的操作,例如

If myVar = 1 Then
....
ElseIf myVar = 2 Then
....
End If

etc.

等等。

I can't really provide more specific advice without knowing more about what your code is attempting to do.

如果不了解您的代码正在尝试做什么,我真的无法提供更具体的建议。

Hope that helps!

希望有帮助!

回答by CallumDA

This should get you on the right track. Loop through your controls and check if they are selected (TRUEin the case of radio buttons)

这应该让你走上正轨。循环浏览您的控件并检查它们是否被选中(TRUE在单选按钮的情况下)

Private Sub CommandButton1_Click()
    For Each Control In UserForm1.Controls
        If Control.Value = True Then
            MsgBox Control.Name
            'MsgBox Control.Tag
        End If
    Next Control
End Sub