Excel - 返回所选选项按钮的标题

时间:2014-01-29 14:34:52

标签: excel vba excel-vba userform

可能是一个简单回答的愚蠢问题,但对于用户形式我是一个真正的新手。

我有“Frame 3”,带有5个不同的选项按钮(Dest1,Dest2,Dest3,Dest4,Dest5)选择一个选项后,所选选项的标题值在哪里存储?如何使用vba访问它。

谢谢你, 约什

5 个答案:

答案 0 :(得分:4)

以下是您可以使用的一些示例代码。将选项按钮添加到组,然后您可以从那里开始。我使用了组,因为你有多个帧,你可以根据组进行检查,并有多个组,并检查为每个组选择哪一个。

Private Sub CommandButton1_Click()
     Dim x As Control

     ' Loop through ALL the controls on the UserForm.
     For Each x In Me.Controls
         ' Check to see if "Option" is in the Name of each control.
         If InStr(x.Name, "Option") Then
             ' Check Group name.
             If x.GroupName = "Grp1" Then
                 ' Check the status of the OptionButton.
                 If x.Value = True Then
                     MsgBox x.Caption
                     Exit For
                 End If
             End If
         End If
     Next
End Sub

答案 1 :(得分:3)

您还可以通过包含它们的框架对象访问选项按钮(如果您有其他框架和控件,您不想通过):

Option Explicit

Sub Test()
    Dim oCtrl As Control

    '***** Try only controls in Frame3
    For Each oCtrl In Frame3.Controls
        '***** Try only option buttons
        If TypeName(oCtrl) = "OptionButton" Then
            '***** Which one is checked?
            If oCtrl.Value = True Then
                '***** What's the caption?
                Debug.Print "You have checked option " & oCtrl.Caption
                Exit For
            End If
        End If
    Next
End Sub

答案 2 :(得分:0)

使用OptionButton1.Caption

可以获得与选项按钮关联的标签文本

如果您正在使用循环,只需将OptionButton1替换为您的变量用于选项按钮,它将在满足条件时通过您需要的那个。例如:

For xitem = 1 To 5
    xFrm = "OptionButton" & xitem
    For Each fItem In Me.Controls
        If fItem.Name Like xFrm Then
                If fItem.Value Then
                    k = fitem.Caption
                End If
        End If
    Next fItem
Next xitem

答案 3 :(得分:0)

就我而言,我希望将选项组中选择的切换标题传递给子表单过滤器。例如选择切换“黑色”过滤器子窗格到strColour =“black”的所有汽车。

我最终得到了这个:

Private Sub OptionGroupName_Click()
    Dim Caption As String
    Caption = OptionGroupName.Controls.Item(OptionGroupName.Value - 1).Caption
    Me.SubformName.Form.Filter = "[SubformField] = """ & Caption & """"
    Me.SubformName.Form.FilterOn = True
End Sub

答案 4 :(得分:0)

不要堆叠其他人的选项,但我创建了一个功能,它接收了无线电组名称并吐出选定的无线电相应的标签标题。在Access而不是Excel中使用它。 只有你提供类似控件的工作.... 即(lblRadioButton1& optRadioButton1)

Function GetSelectedRadioButtonCaption(ByVal optionGroupName As OptionGroup) As String
    Dim oCtrl As Control
    Dim oCtrl2 As Control
    Dim optionLabelName As String
    Dim optionLabelObject As Label
    Dim optionButtonObject As OptionButton

    For Each oCtrl In optionGroupName.Controls
        '***** Try only option buttons
        If TypeOf oCtrl Is OptionButton Then
            '***** Which one is checked?
            Set optionButtonObject = oCtrl
            If optionButtonObject.OptionValue = optionGroupName.Value Then
                '***** What's the caption?
                optionLabelName = Replace(oCtrl.Name, "opt", "lbl")
                For Each oCtrl2 In optionGroupName.Controls
                    If oCtrl2.Name = optionLabelName Then
                      Set optionLabelObject = oCtrl2
                      GetSelectedRadioButtonCaption = optionLabelObject.caption
                      Exit For
                    End If
                Next
            End If
            If GetSelectedRadioButtonCaption <> "" Then
                Exit For
            End If
        End If
    Next
Exit_GetSelectedRadioButtonCaption:
End Function