-1

说明: 在此处输入图像描述 图 1

基于Image1,Level 1是Object,Level 2是object的类型。

在此处输入图像描述
图 2

在图 2 中,如您所见,我分别将它们命名为ComboBox1ComboBox2forLevel 1Level 2

我想要达到的目标:

  1. 当用户选择ComboBox1时,它将显示一组Level 1项目,如图所示,A2:XFD2没有空单元格。
  2. 然后,根据 中的选择ComboBox1,根据 中的值ComboBox2列出如图1Level 2所示。ComboBox1

提前致谢

4

1 回答 1

1

将此添加到您的用户表单代码中:

Private Sub UserForm_Initialize()

Dim rnLevel1 As Range, rnLevel2 As Range, rnTemp As Range

Set rnLevel1 = Sheet1.Cells(2, Sheet1.UsedRange.Columns.Count)

ComboBox2.Clear 'Clear all values first

For Each rnTemp In rnLevel1
    If rnTemp.Value <> "" And rnTemp.Value <> "Level 1" Then ' Add values NOT INCLUDING Level 1 to Combobox1
        ComboBox1.AddItem rnTemp.Value
    End If
Next


End Sub

Private Sub ComboBox1_Change()
Dim rnLevel1 As Range, rnLevel2 As Range, rnTemp As Range

Set rnLevel1 = Sheet1.Cells(2, Sheet1.UsedRange.Columns.Count).Find(ComboBox1.Value) 'Find the correct column
Set rnLevel2 = Sheet1.Range(rnLevel1.Offset(1, 0), Sheet1.Cells(Sheet1.Rows.Count, rnLevel1.Column).End(xlUp))

ComboBox2.Clear 'Clear all values first

For Each rnTemp In rnLevel2
    If rnTemp.Value <> "" Then  ' Add values to Combobox2
        ComboBox2.AddItem rnTemp.Value
    End If
Next

End Sub
于 2013-01-11T10:39:01.050 回答