【问题标题】:Getting a ComboBox to Autocomplete When Typing Something键入内容时让 ComboBox 自动完成
【发布时间】:2020-02-27 18:28:09
【问题描述】:

我正在尝试添加一个搜索框,当用户输入单词时,该搜索框将自动完成名称。我制作的表格列出了我希望显示的所有单词,已在名称工作簿内的另一张表中制作。

这是我正在使用的代码:

Private Sub ComboBox1_Click()

    Dim wbExternal As Workbook 'the other workbook with the data
    Dim wsExternal As Worksheet 'the worksheet in the other workbook
    Dim lnLastRow As Long   'last row on the worksheet
    Dim rngExternal As Range 'range of data from RowSource

    Set wbExternal = Application.Workbooks("New Material Search - 2020 Draft.xlsm")
    Set wsExternal = wbExternal.Worksheets("Man_Prod")
    lngLastRow = wsExternal.Range("A" & wsExternal.Rows.Count).End(xlUp).Row
    Set rngExternal = wsExternal.Range("A2:A" & CStr(lngLastRow))

    ComboBox1 = rngExternal.Address(External:=True)

End Sub

但是,在 ComboBox 中,我得到的只是 '[New Material Search - 2020 Draft.xlsm]Man_Prod'!$A$2:$A$6 和一个错误提示

此公式缺少范围引用或定义的名称。

ComboBox 还应该有一个下拉列表,显示另一张表上的所有单词。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您需要创建一个循环并将外部范围内的所有值添加到ComboBox1。所以你应该使用这样的东西而不是ComboBox1 = rngExternal.Address(External:=True)

    For r in rngExternal
      ComboBox1.AddItem r.Value
    Next r
    

    在这里阅读更多: https://www.contextures.com/Excel-VBA-ComboBox-Lists.html

    【讨论】:

      猜你喜欢
      • 2015-10-27
      • 2011-01-29
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多