【发布时间】:2015-08-06 20:39:54
【问题描述】:
我想要实现的是级联或依赖组合框,在帮助下,我终于在所有 4 个方面都取得了成功。
ComboBox1 = Category
ComboBox2 = Sub Category
ComboBox3 = Location (unique to chosen subcategory)
ComboBox4 = Customer (unique to chosen subcategory and location)
发生的情况是在组合框 4 中,所选位置的所有客户都填充组合框 4,而不是所选位置的所有客户也与子类别一致。
ComboBox1 = cmbRent
ComboBox2 = cmbSub
ComboBox3 = cmbLoc
ComboBox4 = cmbCust
我的所有代码都位于工作表“图表”上。 我的所有数据都位于工作表“数据”上 我所有的组合框都位于“图表”
被引用的数据按方框的顺序分为 4 列。
Column1 = Category
Column2 = Sub Category
Column3 = Location
Column4 = Customer
我觉得我需要引用 cmbSub 和 cmbLoc 中的选择才能实现我想要的?
这是我应用于工作表的所有组合框代码
Private Sub cmbRent_Change()
Dim wsChart As Worksheet
Dim wsData As Worksheet
Dim listOfValues As String 'To store list of values already added
Dim ValueToAdd As String 'To store new value to add
listOfValues = ""
Set wsChart = ThisWorkbook.Sheets("CHART")
Set wsData = ThisWorkbook.Sheets("DATA")
MyVal = Me.cmbRent.Value
'loop thru col B
lr = ThisWorkbook.Sheets("DATA").Cells(Rows.Count, 1).End(xlUp).Row
'clear cmbSub
ThisWorkbook.Sheets("CHART").cmbSub.Clear
For x = 2 To lr
If MyVal = wsData.Cells(x, 1) Then
'add to combobox
ValueToAdd = wsData.Cells(x, 2) 'Get value from worksheet
If InStr(listOfValues, wsData.Cells(x, 2)) = 0 Then
'Check to see if the value has already been added
'If not, add to values added and add the item to the combobox.
listOfValues = listOfValues & ValueToAdd
Me.cmbSub.AddItem ValueToAdd
End If
End If
Next x
ThisWorkbook.Sheets("CHART").cmbSub.ListIndex = -1
End Sub
Private Sub cmbSub_Change()
Dim wsChart As Worksheet
Dim wsData As Worksheet
Dim listOfValues As String 'To store list of values already added
Dim ValueToAdd As String 'To store new value to add
listOfValues = ""
Set wsChart = ThisWorkbook.Sheets("CHART")
Set wsData = ThisWorkbook.Sheets("DATA")
MyVal = ThisWorkbook.Sheets("CHART").cmbSub.Value
'loop thru col c
lr = wsData.Cells(Rows.Count, 2).End(xlUp).Row
ThisWorkbook.Sheets("CHART").cmbLoc.Clear
For x = 2 To lr
If MyVal = wsData.Cells(x, 2) Then
'add to combobox
ValueToAdd = wsData.Cells(x, 3) 'Get value from worksheet
If InStr(listOfValues, wsData.Cells(x, 3)) = 0 Then
'Check to see if the value has already been added
'If not, add to values added and add the item to the combobox.
listOfValues = listOfValues & ValueToAdd
ThisWorkbook.Sheets("CHART").cmbLoc.AddItem ValueToAdd
End If
End If
Next x
ThisWorkbook.Sheets("CHART").cmbLoc.ListIndex = -1
End Sub
Private Sub cmbLoc_Change()
Dim wsChart As Worksheet
Dim wsData As Worksheet
Dim listOfValues As String 'To store list of values already added
Dim ValueToAdd As String 'To store new value to add
listOfValues = ""
Set wsChart = ThisWorkbook.Sheets("CHART")
Set wsData = ThisWorkbook.Sheets("DATA")
MyVal = ThisWorkbook.Sheets("CHART").cmbLoc.Value
'loop thru col D
lr = wsData.Cells(Rows.Count, 3).End(xlUp).Row
ThisWorkbook.Sheets("CHART").cmbCust.Clear
For x = 2 To lr
If MyVal = wsData.Cells(x, 3) Then
'add to combobox
ValueToAdd = wsData.Cells(x, 4) 'Get value from worksheet
If InStr(listOfValues, wsData.Cells(x, 4)) = 0 Then
'Check to see if the value has already been added
'If not, add to values added and add the item to the combobox.
listOfValues = listOfValues & ValueToAdd
ThisWorkbook.Sheets("CHART").cmbCust.AddItem ValueToAdd
End If
End If
Next x
ThisWorkbook.Sheets("CHART").cmbCust.ListIndex = -1
End Sub
如果您想了解更多背景知识,请查看此链接:Excel '13 VBA Cascading ComboBox - Trouble getting unique values in Combobox2
【问题讨论】:
标签: excel combobox activex cascadingdropdown vba