1

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
4

3 に答える 3

2

以下は、これを達成するための私の解決策です。

1 つのループだけに含まれるようにすべてを書き直し、For両方のコンボボックスを更新するように設定しました。

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
于 2012-10-19T21:48:33.343 に答える
1

あなたが達成しようとしていることは明らかではありません。コンボボックスからすべてのエントリをクリアする場合は、これを使用します

Do While Me.combo.ListCount > 0
    Me.combo.RemoveItem(0)
Loop
于 2012-10-19T19:38:22.973 に答える