我想根据另一个用户的选择填写一个下拉列表。我正在尝试通过在选择第一个组合框(cbo_park)后即时添加项目来根据另一个字段的选择来更新字段的内容。
我有四个下拉列表:
第一个下拉 cbo_park 有以下选项:
Central
East
West
我有第二本名为 的工作簿查找室其中包含下表:
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
当用户选择第一个下拉菜单下的中央公园选项时 cbo_park 我只希望中央公园的房间显示在下拉列表中 cbo_prefRoom1 , cbo_prefRoom2 和 cbo_prefRoom3 .我将如何实现这一目标?
请在下面找到我到目前为止的尝试。我不断收到一条错误消息:
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
最佳答案
以下是我实现这一目标的解决方案。
我重写了要包含在一个 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
关于excel - VBA 运行时错误 -214724809 (80070057),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12981033/