VBA运行时错误-214724809(80070057)

我想在另一个用户select的基础上填写一个下拉列表。 我试图通过在第一个combobox(cbo_park)被选中之后dynamic地添加项来基于另一个域中的select来更新字段的内容。

我有一个四下拉列表:

第一个下拉cbo_park有以下select:

Central East West 

我有第二个工作簿叫做lookupRoom其中包含下表:

 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下select中央公园选项时,我只希望中央公园中的房间显示在下拉菜单cbo_prefRoom1cbo_prefRoom2cbo_prefRoom3中 。 我将如何去实现这一目标?

请在下面find我迄今的尝试。 我一直在接收错误: 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 

以下是我的解决scheme来实现这一点。

我重写了所有内容,仅包含在For循环中,并将其设置为更新两个combobox。

 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 

它不清楚你想要达到什么目的。 如果要清除combobox中的所有条目,请使用此选项

 Do While Me.combo.ListCount > 0 Me.combo.RemoveItem(0) Loop 

这里是如何实现它没有VBA和不使用combobox。 Excel单元格具有数据validation,这将像一个combobox。 由于它是电子表格的一部分,因此您不必担心大小和位置。

使用A2的标签“Park”,使用B2作为input单元格。 将B2作为活动单元格,转到数据 – >validation; select“允许List ,然后在“来源”中input“ Central,East,West ”。 试试看,看看你是否喜欢你的新的下拉菜单。

现在的房间“诡计”。

  1. Type ="LookupRoom!A"&MATCH(B2,lookupRoom!B1:B10,0)&":A"&MATCH(B2,lookupRoom!B1:B10,1) into C2
  2. 再次转到B3和我们的数据validation,但这次键入=INDIRECT($C$2)到源input。

尝试一下。 现在你有一个下拉,响应你的公园select。