宏-启用时获取所有控制功能

时间:2019-06-01 01:29:16

标签: excel excel-vba

我有这段代码可以从optionbutton和checkbox获取所有标题,但似乎不起作用。我在下一个错误。

    Dim c As Control, str As String
    Dim d As Control, ctr As String

    For each c in Userform1.Controls
        For each d in Userform1.Controls
            If TypeName(c) ="OptionButton" Then 
                If TypeName(d) = "CheckBox" Then
                    str = str & IIf(c = True, c.Caption & vbCrLf, "")
                    ctr = ctr & IIf(d= True, d.Caption & vbCrLf, "")
                End If
            End If
        Next c
    Next d
    TextBox2.Value = "PV:" & str & ctr
End sub

1 个答案:

答案 0 :(得分:0)

尝试跟随子。希望对您有帮助。

Private Sub CommandButton1_Click()
Dim c As Control, str As String, ctr As String

    For Each c In UserForm1.Controls
        If TypeName(c) = "OptionButton" Then
            If c = True Then
                str = str & c.Caption & vbCrLf
            End If
        End If

        If TypeName(c) = "CheckBox" Then
            If c = True Then
                ctr = ctr & c.Caption & vbCrLf
            End If
        End If
    Next c
    'TextBox2.Value = "PV:" & str & ctr
    MsgBox "PV: " & str & ctr
End Sub