早上好,新年快乐!从Excel中的组中返回所选的单选按钮用户表格
我有一个Excel用户窗体,有一些已组合在一起的无线电(选项)按钮。
是否可以引用单选按钮的GroupName来标识哪个被选中?
我试过使用me.myGroup
,但Excel无法识别它。
如果可能,我希望能够写出类似于;
myVar = me.mygroup
在Excel 2013中这可能吗?
非常感谢
皮特
早上好,新年快乐!从Excel中的组中返回所选的单选按钮用户表格
我有一个Excel用户窗体,有一些已组合在一起的无线电(选项)按钮。
是否可以引用单选按钮的GroupName来标识哪个被选中?
我试过使用me.myGroup
,但Excel无法识别它。
如果可能,我希望能够写出类似于;
myVar = me.mygroup
在Excel 2013中这可能吗?
非常感谢
皮特
如果你设置了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
Set opt = ctrl
If opt.Value Then
Set GetSelectedOptionByGroupName = opt
Exit For
End If
End If
Next ctrl
End Function
这应该让你在正确的轨道上。通过你的控制回路和检查,如果他们被选中(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
等
我真的不能提供更具体的建议,而不知道更多关于你的代码是试图做。
希望有帮助!
感谢这个罗宾,作品一种享受!这是一个耻辱,你必须通过所有的控制循环来做到这一点,我认为这是很好的,只需参考组和返回选定的项目,例如0,1,2,3等 – PeteBradshaw