Excel 如果标题与文本框中的逗号分隔值匹配,则激活Userform框架中的复选框

Excel 如果标题与文本框中的逗号分隔值匹配,则激活Userform框架中的复选框,excel,vba,Excel,Vba,我有一个多用户表单,用户在其中进行多项选择(组合框、列表框和复选框),并将其保存在电子表格中,他们可以编辑从我创建的口述录中保存的内容 我面临的问题是,我不知道如何激活他们之前选择的复选框,就像一个框架中有40个复选框,复选框的标题保存为逗号分隔的文本(见下面的代码) 当前的解决方法是,我今天显示他们以前在文本框中选择的复选框,如果文本框中的任何值与框架中的复选框匹配,查找会更容易吗 Dim chk As Control Dim pm As String, delimiter As String

我有一个多用户表单,用户在其中进行多项选择(组合框、列表框和复选框),并将其保存在电子表格中,他们可以编辑从我创建的口述录中保存的内容

我面临的问题是,我不知道如何激活他们之前选择的复选框,就像一个框架中有40个复选框,复选框的标题保存为逗号分隔的文本(见下面的代码)

当前的解决方法是,我今天显示他们以前在文本框中选择的复选框,如果文本框中的任何值与框架中的复选框匹配,查找会更容易吗

Dim chk As Control
Dim pm As String, delimiter As String
For Each chk In Me.PlanningMarkets2.Controls
    If TypeOf chk Is msforms.CheckBox Then
        If (chk.Value) Then
            pm = pm & delimiter & chk.Caption
            delimiter = ", "
        End If
    End If
Next
With pmSheet
    Cells(lastrow, 7).Value = pm
End With
当用户转到userform的编辑部分搜索其先前保存的输入时,将激活选中的复选框


谢谢大家!

这里有一个简单的例子,可以说明您需要什么。基于带有如下复选框的用户表单:

请看下面用户表单背后的代码。您将看到,
SaveButton\u Click
方法创建一个以逗号分隔的复选框标题列表,并在其中存储一个临时变量。您可以将其存储到工作表或任何地方。
RestoreButton\u单击
方法
将列表分割成一个数组,以便我们可以在
captionlist
中快速搜索标题

希望这有帮助

Option Explicit

Private savedChecks As String

Private Sub ClearButton_Click()
    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is MSForms.CheckBox Then
            '--- all checkboxes are cleared
            ctl.Value = False
        End If
    Next ctl
End Sub

Private Sub CloseButton_Click()
    Me.Hide
End Sub

Private Sub RestoreButton_Click()
    Dim captions() As String
    captions = Split(savedChecks, ",")

    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is MSForms.CheckBox Then
            If CaptionInList(captions, ctl.caption) Then
                ctl.Value = True
            End If
        End If
    Next ctl
End Sub

Private Sub SaveButton_Click()
    savedChecks = vbNullString
    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is MSForms.CheckBox Then
            If ctl.Value = True Then
                '--- only save the checkboxes that are checked
                savedChecks = savedChecks & ctl.caption & ","
            End If
        End If
    Next ctl
End Sub

Private Function CaptionInList(ByRef captionList As Variant, ByVal caption As String) As Boolean
    Dim cap As Variant
    For Each cap In captionList
        If cap = caption Then
            CaptionInList = True
            Exit For
        End If
    Next cap
End Function

这里有一个简单的例子,可以说明您需要什么。基于带有如下复选框的用户表单:

请看下面用户表单背后的代码。您将看到,
SaveButton\u Click
方法创建一个以逗号分隔的复选框标题列表,并在其中存储一个临时变量。您可以将其存储到工作表或任何地方。
RestoreButton\u单击
方法
将列表分割成一个数组,以便我们可以在
captionlist
中快速搜索标题

希望这有帮助

Option Explicit

Private savedChecks As String

Private Sub ClearButton_Click()
    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is MSForms.CheckBox Then
            '--- all checkboxes are cleared
            ctl.Value = False
        End If
    Next ctl
End Sub

Private Sub CloseButton_Click()
    Me.Hide
End Sub

Private Sub RestoreButton_Click()
    Dim captions() As String
    captions = Split(savedChecks, ",")

    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is MSForms.CheckBox Then
            If CaptionInList(captions, ctl.caption) Then
                ctl.Value = True
            End If
        End If
    Next ctl
End Sub

Private Sub SaveButton_Click()
    savedChecks = vbNullString
    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is MSForms.CheckBox Then
            If ctl.Value = True Then
                '--- only save the checkboxes that are checked
                savedChecks = savedChecks & ctl.caption & ","
            End If
        End If
    Next ctl
End Sub

Private Function CaptionInList(ByRef captionList As Variant, ByVal caption As String) As Boolean
    Dim cap As Variant
    For Each cap In captionList
        If cap = caption Then
            CaptionInList = True
            Exit For
        End If
    Next cap
End Function

我不太明白你到底想达到什么目的。这里有不同的选项卡,一个是你点击复选框,另一个是你以某种方式反映所做的选择?我真的不太明白你到底想实现什么。有不同的选项卡,一个用于单击复选框,另一个用于反映以某种方式做出的选择?