Reputation: 13216
I want fill a drop down list based on user selection in another. Can't find anything related to it online.
I have a workbook called lookupDept containing the following table:
A B
== ==============================
BS Business School
CG Chemical Engineering
where column A has the defined name deptCode and column B has the defined name deptName. I have a second workbook called lookupModule which contains the following table:
A B C
====== ================================================== ==
BSA005 Organisational Behaviour BS
BSA007 Skills for Study BS
CGA001 Fluid Mechanics I MEng & BEng Status CG
CGA002 Stagewise Processes CG
I'm trying to update cbo_moduleCode on my form to select a range where column A in lookupDept matches column C in lookupModule. This is the code I'm using:
So if the user picks BS - Business School on the form (which is extracted from the lookupDept workbook, I want all the fields containing BS in column C of the lookupModule workbook to selected. This is the code I'm using so far:
Private Sub UserForm_Initialize()
Dim c_deptCode As Range
Dim c_deptName As Range
Dim deptCodes As Variant
Dim deptNames As Variant
Dim ws_dept As Worksheet
Dim ws_misc As Worksheet
Set ws_dept = Worksheets("lookupDept")
Set ws_misc = Worksheets("lookupMisc")
' Assign each range to an array containing the values
deptCodes = Choose(1, ws_dept.Range("deptCode"))
deptNames = Choose(1, ws_dept.Range("deptName"))
' Create deptcode+deptname cbo
For i = 1 To ws_dept.Range("deptCode").Rows.Count
CombinedName = deptCodes(i, 1) & " - " & deptNames(i, 1)
cbo_deptCode.AddItem CombinedName
Next i
End Sub
Upvotes: 0
Views: 444
Reputation: 5962
You can try adding the items on the fly once the first combobox is selected. This code is a sample that might lead you in the right direction:
Private Sub cbo_deptCode_Change()
Dim lLoop As Long, rgLoop As range
For lLoop = 1 To Me.cbo_moduleCode.ListCount
Me.cbo_moduleCode.RemoveItem 0
Next lLoop
Sheets("lookupModule").[a1].CurrentRegion.AutoFilter
Sheets("lookupModule").[a1].CurrentRegion.AutoFilter Field:=3, Criteria1:=Left(Me.cbo_deptCode.Value, 2)
For Each rgLoop In Sheets("lookupModule").[a1].CurrentRegion.Offset(1).SpecialCells(xlCellTypeVisible).Columns(1).Cells
If Len(rgLoop) > 0 Then
Me.cbo_moduleCode.AddItem rgLoop & " - " & rgLoop.Offset(, 1)
End If
Next rgLoop
End Sub
Upvotes: 1