Return 从 Excel 用户表单中的组中选择了单选按钮
Return selected radiobutton from group in Excel userform
我有一个 Excel 用户表单,其中有许多单选(选项)按钮组合在一起。
是否可以参考单选按钮的 GroupName 来识别选择了哪一个?
我试过me.myGroup
,但是Excel不认识。
如果可以的话,我想这样写;
myVar = me.mygroup
这在 Excel 2013 年可能吗?
这应该会让您走上正轨。遍历您的控件并检查它们是否被选中(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
早上好皮特,
您需要为您的变量分配一个特定的值以确定哪个按钮被点击。
试试
Private Sub OptionButton1_Click()
myVar = 1
End Sub
使用特定值。您可以通过双击用户窗体编辑器中的单选按钮自动访问此子例程。这样,稍后在您的代码中您可以引用 myVar 来确定您的脚本接下来应该执行的操作,例如
If myVar = 1 Then
....
ElseIf myVar = 2 Then
....
End If
等等
如果不进一步了解您的代码试图做什么,我真的无法提供更具体的建议。
希望对您有所帮助!
如果您在选项按钮上设置了 GroupName
属性,如下所示:
然后您可以在控件的循环中引用 属性 以查看控件的 TypeName
是 OptionButton
并且 GroupName
是否匹配:
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
我有一个 Excel 用户表单,其中有许多单选(选项)按钮组合在一起。
是否可以参考单选按钮的 GroupName 来识别选择了哪一个?
我试过me.myGroup
,但是Excel不认识。
如果可以的话,我想这样写;
myVar = me.mygroup
这在 Excel 2013 年可能吗?
这应该会让您走上正轨。遍历您的控件并检查它们是否被选中(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
早上好皮特,
您需要为您的变量分配一个特定的值以确定哪个按钮被点击。
试试
Private Sub OptionButton1_Click()
myVar = 1
End Sub
使用特定值。您可以通过双击用户窗体编辑器中的单选按钮自动访问此子例程。这样,稍后在您的代码中您可以引用 myVar 来确定您的脚本接下来应该执行的操作,例如
If myVar = 1 Then
....
ElseIf myVar = 2 Then
....
End If
等等
如果不进一步了解您的代码试图做什么,我真的无法提供更具体的建议。
希望对您有所帮助!
如果您在选项按钮上设置了 GroupName
属性,如下所示:
然后您可以在控件的循环中引用 属性 以查看控件的 TypeName
是 OptionButton
并且 GroupName
是否匹配:
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