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
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: