问题
I want fill a drop down list based on user selection in another. I'm trying to update the contents of a field based on selection in another by adding the items on the fly once the first combobox (cbo_park) is selected.
I have a four drop down lists:
The first drop down cbo_park has the following options:
Central
East
West
I have a second workbook called lookupRoom which contains the following table:
roomCode    park
A.0.01      Central 
A.2.01      Central 
A.3.01      Central 
HE.0.10     East
HE.0.21     East
HE.0.22     East
KG.1.07     West
KG.1.09     West
KG.1.10     West
When the user selects the Central park option under the first drop down cbo_park I only want rooms in the Central park to be displayed in dropdown cbo_prefRoom1, cbo_prefRoom2 and cbo_prefRoom3. How would I go about achieving this?
Please find below my attempts so far. I keep receiving an error at the line: Me.cbo_prefRoom1.RemoveItem 0.
Private Sub cbo_park_Change()
Dim lLoop As Long, rgLoop As Range
    For lLoop = 1 To Me.cbo_park.ListCount
        Me.cbo_prefRoom1.RemoveItem 0
    Next lLoop
    Sheets("lookupRoom").[a1].CurrentRegion.AutoFilter
    Sheets("lookupRoom").[a1].CurrentRegion.AutoFilter Field:=3, Criteria1:=Left(Me.cbo_park.Value, 2)
    For Each rgLoop In Sheets("lookupRoom").[a1].CurrentRegion.Offset(1).SpecialCells(xlCellTypeVisible).Columns(1).Cells
        If Len(rgLoop) > 0 Then
            Me.cbo_prefRoom1.AddItem rgLoop
        End If
    Next rgLoop
End Sub
    回答1:
Below is my solution for achieving this.
I rewrote everything to be contained in just the one For loop and set it to update both comboboxes.
Private Sub cbo_park_Change()
    Dim lLoop As Long
    '- clear the two comboboxes we are about to update
    Me.cbo_prefRoom1.Clear
    Me.cbo_prefRoom3.Clear
    '- loop through the worksheet and test each row
    For lLoop = 1 To Sheets("lookupRoom").Range("A" & Sheets("lookupRoom").Rows.Count).End(xlUp).Row
        '- if the row's column C matches the combobox then add the corresponding values to other combos
        If Sheets("lookupRoo"m).Range("C" & lLoop).Value = Me.cbo_park.Value Then
            Me.cbo_prefRoom1.AddItem Sheets("lookupRoom").Range("B" & lLoop).Value
            Me.cbo_prefRoom2.AddItem Sheets("lookupRoom").Range("B" & lLoop).Value
        End If
    Next lLoop
End Sub
    回答2:
Its not clear what you are trying to achieve. If it's to clear all entries from a combo box, use this
Do While Me.combo.ListCount > 0
    Me.combo.RemoveItem(0)
Loop
    回答3:
Here's how to implement it without VBA and without using the combo box. Excel cells has Data Validation which would act like a combo box. Since it part of the spreadsheet you don't have to worry about sizing and positioning.
With the label "Park" in A2, use B2 as your input cell. With B2 as your active cell, go to Data -> Validation; choose List for Allow and just type Central,East,West in the Source. Try it out and see if you like your new drop down.
Now for the Rooms "trickery".
- Type 
="LookupRoom!A"&MATCH(B2,lookupRoom!B1:B10,0)&":A"&MATCH(B2,lookupRoom!B1:B10,1)intoC2. - Go to 
B3and us data validation again, but this time type=INDIRECT($C$2)into the source input. 
Try it. Now you have a drop down that responds to your park selection.
来源:https://stackoverflow.com/questions/12981033/vba-run-time-error-214724809-80070057