Vba 跨多个列表框设置列表框计数条件

Vba 跨多个列表框设置列表框计数条件,vba,excel,Vba,Excel,我有一个在excel中为最终用户构建的工具,用于为工作面试生成面试表,为了实现这一点,我构建了一系列问题,确定了角色所需技能的标准,这些技能转化为与这些技能相关的问题。选择的工作面试问题一次最多5个 我有一些代码记录了在一个列表框中选择了多少个列表框,不幸的是,由于用户体验问题,我们不得不重新设计界面。我不得不用自己的列表框生成多个用户表单,每个列表框通过链接到数据透视表条件的动态命名范围生成其列表。有人知道如何跟踪多个列表框中的选择吗?下面的当前代码适用于单个列表框 Private Sub L

我有一个在excel中为最终用户构建的工具,用于为工作面试生成面试表,为了实现这一点,我构建了一系列问题,确定了角色所需技能的标准,这些技能转化为与这些技能相关的问题。选择的工作面试问题一次最多5个

我有一些代码记录了在一个列表框中选择了多少个列表框,不幸的是,由于用户体验问题,我们不得不重新设计界面。我不得不用自己的列表框生成多个用户表单,每个列表框通过链接到数据透视表条件的动态命名范围生成其列表。有人知道如何跟踪多个列表框中的选择吗?下面的当前代码适用于单个列表框

Private Sub ListBox1_Change()
Dim counter         As Integer
Dim selectedCount   As Integer

selectedCount = 0

For counter = 1 To ListBox1.ListCount Step 1
    If ListBox1.Selected(counter - 1) = True Then
        selectedCount = selectedCount + 1
    End If
Next counter

If selectedCount >= 6 Then
    ListBox1.Selected(ListBox1.ListIndex) = False
    MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
End If

End Sub

所以我设法重写了它,使它能够跨位于不同用户窗体中的多个列表框工作,我把这段代码放在包含列表框的所有用户窗体中,并且它能够工作

Private Sub ListBox1_Change()
Dim counter1 As Integer, counter2 As Integer, counter3 As Integer, counter4 As Integer, counter5 As Integer, counter6 As Integer, selectedCount As Integer
selectedCount = 0

For counter = 1 To FirstQuestion.ListBox1.ListCount Step 1
    If FirstQuestion.ListBox1.Selected(counter - 1) = True Then
        selectedCount = selectedCount + 1
        If selectedCount >= 6 Then
            FirstQuestion.ListBox1.Selected(FirstQuestion.ListBox1.ListIndex) = False
            MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
            Exit Sub
        End If
    End If
Next counter

For counter2 = 1 To SecondQuestion.ListBox1.ListCount Step 1
    If SecondQuestion.ListBox1.Selected(counter2 - 1) = True Then
        selectedCount = selectedCount + 1
        If selectedCount >= 6 Then
            SecondQuestion.ListBox1.Selected(SecondQuestion.ListBox1.ListIndex) = False
            MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
            Exit Sub
        End If
    End If
Next counter2

For counter3 = 1 To ThirdQuestion.ListBox1.ListCount Step 1
    If ThirdQuestion.ListBox1.Selected(counter3 - 1) = True Then
        selectedCount = selectedCount + 1
        If selectedCount >= 6 Then
            ThirdQuestion.ListBox1.Selected(ThirdQuestion.ListBox1.ListIndex) = False
            MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
            Exit Sub
        End If
    End If
Next counter3

For counter4 = 1 To FourthQuestion.ListBox1.ListCount Step 1
    If FourthQuestion.ListBox1.Selected(counter4 - 1) = True Then
        selectedCount = selectedCount + 1
        If selectedCount >= 6 Then
            FourthQuestion.ListBox1.Selected(FourthQuestion.ListBox1.ListIndex) = False
            MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
            Exit Sub
        End If
    End If
Next counter4

For counter5 = 1 To FifthQuestion.ListBox1.ListCount Step 1
    If FifthQuestion.ListBox1.Selected(counter5 - 1) = True Then
        selectedCount = selectedCount + 1
        If selectedCount >= 6 Then
            FifthQuestion.ListBox1.Selected(FifthQuestion.ListBox1.ListIndex) = False
            MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
            Exit Sub
        End If
    End If
Next counter5

For counter6 = 1 To SixthQuestion.ListBox1.ListCount Step 1
    If SixthQuestion.ListBox1.Selected(counter6 - 1) = True Then
        selectedCount = selectedCount + 1
        If selectedCount >= 6 Then
            SixthQuestion.ListBox1.Selected(SixthQuestion.ListBox1.ListIndex) = False
            MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
            Exit Sub
        End If
    End If
Next counter6
End Sub

您的列表框是否分布在多个用户表单上?是的,我将它们分布在6个用户表单上,但它们都命名为“Listbox1”。您可以使用它们所在对象的名称引用它们吗:form1.Listbox1、form2.Listbox1等。谢谢您,我重新编写了代码,完全按照您所说的做,请检查我的答案