0

可能重复:
根据另一个选择列表动态更新选择列表

我正在努力根据另一个用户的选择来填写下拉列表。在网上找不到任何相关的东西我有一个名为lookupDept的工作簿,其中包含下表:

A   B
==  ==============================
BS  Business School
CD  Design and Technology
CG  Chemical Engineering
CM  Chemistry
CO  Computer Science
CV  Civil and Building Engineering

其中列 A具有定义的名称deptCode列 B具有定义的名称deptName。我有第二个名为lookupModule的工作簿,其中包含下表:

A       B                                                   C
======  ==================================================  ==
BSA005  Organisational Behaviour                            BS
BSA007  Skills for Study                                    BS
CGA001  Fluid Mechanics I MEng & BEng Status                CG
CGA002  Stagewise Processes                                 CG
CMA001  Energetics and Rates of Change                      CM
CMA002  Structure and Reactivity in Inorganic Chemistry     CM
COA101  Essential Skills for Computing                      CO
COA107  Logic and Functional Programming                    CO
CVA001  Communication                                       CV
CVA002  Fluid Mechanics                                     CV

我正在尝试更新表单上的cbo_moduleCode以选择一个范围,其中lookupDept中的 A 列lookupModule中的C列匹配。这是我正在使用的代码:

因此,如果用户在表单上选择BS - Business School(从lookupDept工作簿中提取,我希望选中lookupModule工作簿的 C 列中包含BS的所有字段。这是我目前使用的代码:

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
4

0 回答 0