Hi All
I would like to setup an userform having multiple dependent comboboxes.
Data is retrieved from the Sheets(“Summary”):
column A is duplicate value of portfolio names
column C is duplicate value of project names
column E to AA to show the budget data for the selectedproject.
I have tried the following code to filter portfolio andproject names, however only the portfolio name filter works, but not theproject name.
I just copied code into my project, so I hope someone cantell me what I have done wrong, and what is the best way to do after a projectname is selected from the combobox, then the budget data is shown foredit? Many thanks.
I would like to setup an userform having multiple dependent comboboxes.
Data is retrieved from the Sheets(“Summary”):
column A is duplicate value of portfolio names
column C is duplicate value of project names
column E to AA to show the budget data for the selectedproject.
I have tried the following code to filter portfolio andproject names, however only the portfolio name filter works, but not theproject name.
I just copied code into my project, so I hope someone cantell me what I have done wrong, and what is the best way to do after a projectname is selected from the combobox, then the budget data is shown foredit? Many thanks.
Code:
Private Sub UserForm_Initialize()
Dim lr As Long, i As Long
Dim dic As Object
Dim arr As Variant
With Sheets("Summary")
lr = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
'load array for speed
arr = .Range("A3:A" & lr)
End With
Set dic = CreateObject("Scripting.Dictionary")
'use dictionary for uniques
For i = 1 To UBound(arr, 1)
dic(arr(i, 1)) = 1
Next i
'populate combo
Me.ComboBox1.List = Application.Transpose(dic.keys)
End Sub
Private Sub ComboBox1_Change()
Dim lr As Long, i As Integer
Dim filtRng As Range, cel As Range
Application.ScreenUpdating = False
'remove existing list from combobox2
With Me.ComboBox2
.Value = ""
For i = .ListCount - 1 To 0 Step -1
.RemoveItem i
Next i
End With
'establish range to filter
With Sheets("Summary")
lr = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Set filtRng = .Range("C3:C" & lr)
End With
'filter the range
filtRng.AutoFilter field:=1, Criteria1:=Me.ComboBox1.Value, VisibleDropDown:=False
'add items to combo list
For Each cel In filtRng.Offset(1, 2).SpecialCells(xlCellTypeVisible).Cells
If cel.Value <> "" Then
Me.ComboBox2.AddItem cel.Value
End If
Next cel
'remove filter
filtRng.AutoFilter
Application.ScreenUpdating = True
End Sub