【问题标题】:Autocomplete suggestion in Excel data validation list againExcel数据验证列表中的自动完成建议再次
【发布时间】:2019-06-06 05:48:23
【问题描述】:

如何在输入时在 Excel 数据验证列表中提出建议。我的请求中有一些限制:

  1. 项目列表应该在另一个工作表中,并且不能在隐藏行的上方。
  2. 键入短语应将列表缩小到包含该短语的所有项目。
  3. 搜索应该不区分大小写。

所以在输入am 之后,假设我们应该有一个从AmeliaCamilaSamantha 接听的建议,前提是这些女孩的名字在项目列表中。

我找到了一个很好的解决方案here,但是它不会过滤带有contains 子句而是begins with 的项目。我很快在这里总结了建议的解决方案。

  1. 我们在工作表中插入一个组合框(ActiveX 控件)。
  2. 我们右键单击一个工作表名称>查看代码>并将VBA代码粘贴到工作表VBA编辑器中:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Update by Extendoffice: 2018/9/21
        Dim xCombox As OLEObject
        Dim xStr As String
        Dim xWs As Worksheet
        Dim xArr
        Set xWs = Application.ActiveSheet
        On Error Resume Next
        Set xCombox = xWs.OLEObjects("TempCombo")
        With xCombox
            .ListFillRange = ""
            .LinkedCell = ""
            .Visible = False
        End With
        If Target.Validation.Type = 3 Then
            Target.Validation.InCellDropdown = False
            Cancel = True
            xStr = Target.Validation.Formula1
            xStr = Right(xStr, Len(xStr) - 1)
            If xStr = "" Then Exit Sub
            With xCombox
                .Visible = True
                .Left = Target.Left
                .Top = Target.Top
                .Width = Target.Width + 5
                .Height = Target.Height + 5
                .ListFillRange = xStr
                If .ListFillRange = "" Then
                    xArr = Split(xStr, ",")
                    Me.TempCombo.List = xArr
                End If
                .LinkedCell = Target.Address
            End With
            xCombox.Activate
            Me.TempCombo.DropDown
        End If
    End Sub
    
    Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        Select Case KeyCode
            Case 9
                Application.ActiveCell.Offset(0, 1).Activate
            Case 13
                Application.ActiveCell.Offset(1, 0).Activate
        End Select
    End Sub
    

我找不到将搜索选项从“开始于”更改为contains 的修改方法。

关于验证列表中的自动完成或自动提示的问题已经被问到了。
Excel data validation with suggestions/autocomplete
Excel 2010: how to use autocomplete in validation list
但它们都没有包含满足我施加的限制的答案。

供下载的测试文件是here

【问题讨论】:

  • 一般来说,数据验证和 ComboBox 都不支持你的要求。我唯一的想法是在循环中从xArr 中删除所有不包含键入字符串的项目,例如am。并在每个 TempCombo_KeyDown 上刷新 ComboBox 列表。但是如果列表很长,这很容易变得很慢。
  • @PEH 你能把它作为答案吗?哪个变量保存当前键入的短语?

标签: excel vba validation autocomplete autosuggest


【解决方案1】:

尝试添加以下事件(另外 2 个)。每次输入内容时,代码都会刷新 ComboBox 列表。

Private Sub TempCombo_Change()
    With Me.TempCombo
        If Not .Visible Then Exit Sub
        .Clear 'needs property MatchEntry set to 2 - fmMatchEntryNone
        .Visible = False 'to refresh the drop down
        .Visible = True
        .Activate
        Dim xStr As String, xArr As Variant
        xStr = TempCombo.TopLeftCell.Validation.Formula1
        xStr = Right(xStr, Len(xStr) - 1)
        xArr = Split(xStr, Application.International(xlListSeparator))
        Dim itm As Variant
        For Each itm In xArr
            If InStr(1, itm, .Value, vbTextCompare) > 0 Or .Value = "" Then
                .AddItem itm
            End If
        Next itm
        .DropDown
    End With
End Sub

【讨论】:

  • 在行中弹出错误:.List = Array() 和消息Permission denied。什么是数组()?它是空的。
  • @PrzemyslawRemin 无法复制。我创建了一个新工作簿,提取了您的宏,编写了我的宏,添加了一个新的 ActiveX 组合框并将其命名为 TempCombo,并向单元格添加了一个数据验证列表。就是这样,它奏效了。
  • 什么是数组()?它是一个变量吗?或者任何存储数据的东西?它指的是什么?它应该在事件触发之前存储任何数据吗?
  • @PrzemyslawRemin Array() 是一个空数组来重置列表。
  • 我得到了蠕虫。你的代码工作正常。但是,如果您更改数据验证列表的来源,就会出现问题。您的列表被定义为Amelia,Camila,Samantha,如果您将其更改为=items!$A$1:$A$41(项目是工作表名称),那么您将遇到我遇到的所有奇怪行为。
【解决方案2】:

为了克服你的第一个限制,也许你可以为你的组合框分配一个范围:

Dim xCombox             As OLEObject
    Dim xStr                As String
    Dim xWs                 As Worksheet
    Dim xArr
    Dim i                   As Range

    Set xWs = Application.ActiveSheet
    On Error Resume Next
    Set xCombox = xWs.OLEObjects("Combotest")
    With Sheets("Test_list2")
    Set i = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
    End With
    Combotest.ListFillRange = i.Address
 Set xWs = Application.ActiveSheet
    On Error Resume Next
    Set xCombox = xWs.OLEObjects("Combotest")
    With xCombox
        .LinkedCell = "F2"
        .Visible = True
    End With
.
.
.
.
End Sub

【讨论】:

    猜你喜欢
    • 2013-11-18
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2014-11-26
    • 2015-09-10
    • 2022-11-27
    • 2018-03-03
    • 1970-01-01
    相关资源
    最近更新 更多