excel - VBA 运行时错误 -214724809 (80070057)

标签 excel vba

我想根据另一个用户的选择填写一个下拉列表。我正在尝试通过在选择第一个组合框(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/

相关文章:

excel - For循环捕获不同命名范围内的值

excel - 当打开另一个具有相似工作表的工作簿时,VBA 用户定义函数返回#VALUE

vba - 如何在Excel VBA中通过选择案例?

arrays - "Text to Column - Fixed Width"的 VBA 代码 - 循环

vba - 在 Excel-Vba 中复制多列

javascript - Angular2/Javascript 表导出到 Excel

excel - 将 Excel 工作表复制为值/图片,而不引用其他工作表

excel - VLOOKUP 并填写日期范围内的所有单元格

vba - Excel vba向单元格添加空格以达到一定长度

vba - SoapClient30 在窗口 7 64 位中给出错误