根据第二个列表框选择为第三个列表框生成选项

Generate options for 3rd ListBox based on 2nd ListBox selection

我写了一个简单版本的用户表单来查明我的问题。

ListBox1 lstClassName 的选择决定了 ListBox2 lstClassName 的选项(有效)。

ListBox2 的选择应该决定 ListBox3 的选项lstLanguage(无效)。

类似问题的答案比我需要此用户表单做的更多。

我将文件上传到 Google 驱动器。您可以看到该表单是如何工作的。 Link To Excel File

Option Explicit
Public ClassX As Integer
Public LanguageX As Integer

Private Sub UserForm_Initialize()
    With lstClassName
        .AddItem "Cooking"
        .AddItem "Art"
        .AddItem "Music"
    End With
End Sub
    
    
Private Sub lstClassName_Click()
    ClassX = lstClassName.ListIndex
    Select Case ClassX
    Case Is = 0 'Cooking Class
        lstLanguage.Clear
        lstLanguage.AddItem "English"
        lstLanguage.AddItem "Spanish"
     Case Is = 1 'Art Class
        lstLanguage.Clear
        lstLanguage.AddItem "English"
        lstLanguage.AddItem "French"
    Case Is = 2 'Music Class
        lstLanguage.Clear
        lstLanguage.AddItem "English"
        lstLanguage.AddItem "Spanish"
        lstLanguage.AddItem "French"
    End Select
End Sub
    
Private Sub lstLanguage_Click()
    LanguageX = lstLanguage.ListIndex
    Select Case LanguageX
    Case (ClassX = 0 And LanguageX = 0) 'Cooking Class in English
        lstDay.Clear
        lstDay.AddItem "Monday"
        lstDay.AddItem "Wednesday"
    Case (ClassX = 0 And LanguageX = 1) 'Cooking Class in Spanish
        lstDay.Clear
        lstDay.AddItem "Monday"
        lstDay.AddItem "Thursday"
    Case (ClassX = 1 And LanguageX = 0) 'Art Class in English
        lstDay.Clear
        lstDay.AddItem "Tuesday"
        lstDay.AddItem "Friday"
    Case (ClassX = 1 And LanguageX = 1) 'Art Class in French
        lstDay.Clear
        lstDay.AddItem "Wednesday"
        lstDay.AddItem "Thursday"
    Case (ClassX = 2 And LanguageX = 0) 'Music Class in English
        lstDay.Clear
        lstDay.AddItem "Monday"
        lstDay.AddItem "Friday"
    Case (ClassX = 2 And LanguageX = 1) 'Music Class in Spanish
        lstDay.Clear
        lstDay.AddItem "Tuesday"
        lstDay.AddItem "Wednesday"
    Case (ClassX = 2 And LanguageX = 2) 'Music Class in French
        lstDay.Clear
        lstDay.AddItem "Thursday"
        lstDay.AddItem "Friday"
    End Select
End Sub
    
Private Sub CommandButton1_Click()
    'This would then be the button to add data to a part of the spreadsheet
End Sub

注意:我没有费心完成命令按钮的代码或如何处理来自该用户表单的数据。

与Case语句有关。不知道具体是什么,因为我不经常使用它,但如果你有多个条件,你通常更适合使用 If。也可以考虑把点击事件改成change事件。使用此代码应该可以。

Option Explicit
Public ClassX As Integer
Public LanguageX As Integer



Private Sub UserForm_Initialize()

With lstClassName
        .AddItem "Cooking"
        .AddItem "Art"
        .AddItem "Music"
End With


End Sub


Private Sub lstClassName_Click()

ClassX = lstClassName.ListIndex

Select Case ClassX
    Case Is = 0 'Cooking Class
        lstLanguage.Clear
        lstLanguage.AddItem "English"
        lstLanguage.AddItem "Spanish"

    Case Is = 1 'Art Class
        lstLanguage.Clear
        lstLanguage.AddItem "English"
        lstLanguage.AddItem "French"

    Case Is = 2 'Music Class
        lstLanguage.Clear
        lstLanguage.AddItem "English"
        lstLanguage.AddItem "Spanish"
        lstLanguage.AddItem "French"
End Select

lstDay.Clear


End Sub

Private Sub lstLanguage_Click()

LanguageX = lstLanguage.ListIndex

If ClassX = 0 And LanguageX = 0 Then 'Cooking Class in English
        lstDay.Clear
        lstDay.AddItem "Monday"
        lstDay.AddItem "Wednesday"
    ElseIf ClassX = 0 And LanguageX = 1 Then 'Cooking Class in Spanish
        lstDay.Clear
        lstDay.AddItem "Monday"
        lstDay.AddItem "Thursday"
    ElseIf ClassX = 1 And LanguageX = 0 Then 'Art Class in English
        lstDay.Clear
        lstDay.AddItem "Tuesday"
        lstDay.AddItem "Friday"
    ElseIf ClassX = 1 And LanguageX = 1 Then 'Art Class in French
        lstDay.Clear
        lstDay.AddItem "Wednesday"
        lstDay.AddItem "Thursday"
    ElseIf ClassX = 2 And LanguageX = 0 Then 'Music Class in English
        lstDay.Clear
        lstDay.AddItem "Monday"
        lstDay.AddItem "Friday"
    ElseIf ClassX = 2 And LanguageX = 1 Then 'Music Class in Spanish
        lstDay.Clear
        lstDay.AddItem "Tuesday"
        lstDay.AddItem "Wednesday"
    ElseIf ClassX = 2 And LanguageX = 2 Then 'Music Class in French
        lstDay.Clear
        lstDay.AddItem "Thursday"
        lstDay.AddItem "Friday"
End If


End Sub



Private Sub CommandButton1_Click()

End Sub

此处 lstLanguage_Click() 的创意替代品。由于您只有 2 个变量,您可以将它们转换为十进制值以便于编码。 ClassX 将是整数部分,LanguageX 将是小数部分。

Private Sub lstLanguage_Click()
    Dim uCode As Double
    LanguageX = lstLanguage.ListIndex
    uCode = CDbl(ClassX) + CDbl(LanguageX) / 10
    lstDay.Clear ' This is done for any one clicked
    Select Case uCode
        Case 0# ' (ClassX = 0 And LanguageX = 0) 'Cooking Class in English
            lstDay.AddItem "Monday"
            lstDay.AddItem "Wednesday"
        Case 0.1 ' (ClassX = 0 And LanguageX = 1) 'Cooking Class in Spanish
            lstDay.AddItem "Monday"
            lstDay.AddItem "Thursday"
        Case 1#  ' (ClassX = 1 And LanguageX = 0) 'Art Class in English
            lstDay.AddItem "Tuesday"
            lstDay.AddItem "Friday"
        Case 1.1 ' (ClassX = 1 And LanguageX = 1) 'Art Class in French
            lstDay.AddItem "Wednesday"
            lstDay.AddItem "Thursday"
        Case 2#  ' (ClassX = 2 And LanguageX = 0) 'Music Class in English
            lstDay.AddItem "Monday"
            lstDay.AddItem "Friday"
        Case 2.1 ' (ClassX = 2 And LanguageX = 1) 'Music Class in Spanish
            lstDay.AddItem "Tuesday"
            lstDay.AddItem "Wednesday"
        Case 2.2 ' (ClassX = 2 And LanguageX = 2) 'Music Class in French
            lstDay.AddItem "Thursday"
            lstDay.AddItem "Friday"
    End Select
End Sub

如果您喜欢坚持使用的方式,修复很简单。
Select Case LanguageX 更改为 Select Case True.

您可以使用 Dictionary 对象提供更快的解决方案。 您可以使用 Dictionary 对象提供更快的解决方案。 首先,为列创建动态字段名称。 3个列表框被添加到用户窗体,代码被添加到依赖列表框。

源代码和示例工作簿here