Excel 1.如何使用VBA为动态范围自动完成和自动建议下拉列表

Excel 1.如何使用VBA为动态范围自动完成和自动建议下拉列表,excel,vba,Excel,Vba,我正试图开发一个代码,可以帮助我创建一个下拉列表与自动建议和自动完成的选项被过滤在列表中。 例如,如果键入“a”,则下拉列表应显示包含“a”的所有选项的结果,一旦键入“ab”,下拉选项应自动过滤掉。数据是从数据库中提取的,因此范围必须是动态的,并且应该使用VBA在下拉列表中自动更新 我在网上试过几段代码,但有些东西不见了,而且很久以来一直存在这个问题。该功能适用于excel公式,但我需要通过VBA实现 If target.Validation.Type = xlValidateList Then

我正试图开发一个代码,可以帮助我创建一个下拉列表与自动建议和自动完成的选项被过滤在列表中。 例如,如果键入“a”,则下拉列表应显示包含“a”的所有选项的结果,一旦键入“ab”,下拉选项应自动过滤掉。数据是从数据库中提取的,因此范围必须是动态的,并且应该使用VBA在下拉列表中自动更新

我在网上试过几段代码,但有些东西不见了,而且很久以来一直存在这个问题。该功能适用于excel公式,但我需要通过VBA实现

If target.Validation.Type = xlValidateList Then
   ' Added this to auto select all text when activating the combox box.
xCombox.SetFocus

With xCombox
    .ListFillRange = vbNullString
    .LinkedCell = vbNullString
    .Visible = False
End With


Dim xStr As String
Dim xArr


If target.Validation.Type = xlValidateList Then
    ' The target cell contains Data Validation.

    target.Validation.InCellDropdown = False


    ' Cancel the "SelectionChange" event.
 Dim Cancel As Boolean
    Cancel = True


    xStr = target.Validation.Formula1
    xStr = Right(xStr, Len(xStr) - 1)

    If xStr = vbNullString 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 = vbNullString Then
            xArr = Split(xStr, ",")
            TempCombo.List = xArr
        End If
         .LinkedCell = target.Address

    End With

    xCombox.Activate
    Me.TempCombo.DropDown
欢迎提出任何建议和解决方案


将。

视为OP提到的尝试使用组合框显示过滤后的数据…:

在我的项目中,我有一个
文本框
和一个
列表框
(后者也可以是一个
组合框
)。文本框是打开的,用户可以键入过滤器,而列表框则显示从数据库导入的过滤项

文本框(在本例中命名为
machbox
)具有一个可更新列表框的:

Private Sub machbox_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

'If what has been typed is a number / Delete / Backspace
If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Or (47 < KeyCode And KeyCode < 58) Or KeyCode Like vbKeyNumpad & "*" Then
    If Len(Me.machbox.Value) > 3 Then nlist 'prevents the query from firing when the filter is too broad
End If
End Sub
Sub nlist

'Set F as the filter, which is the value of the listbox
F = machbox.Value

'I have ommited the code that connects to the DB

'Clear the listbox so it won't keep the records from the previous filter
Me.ListBox1.Clear

'Execute the query and parse the records to the listbox
Set rs = cnn.Execute("Select * From Table Where Code = '" & F & "'") 'example query
If (rs.RecordCount <> 0) Then
   Do While Not rs.EOF
      Me.ListBox1.AddItem (rs.Fields(0).Value)
      rs.MoveNext
   Loop
End If

End Sub
Private Sub-machbox\u KeyUp(ByVal KeyCode作为MSForms.ReturnInteger,ByVal Shift作为Integer)
'如果键入的是数字/删除/退格
如果KeyCode=vbKeyBack或KeyCode=vbKeyDelete或(473,则nlist'防止在筛选器太宽时触发查询
如果结束
端接头
然后,填充列表框的子对象:

Private Sub machbox_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

'If what has been typed is a number / Delete / Backspace
If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Or (47 < KeyCode And KeyCode < 58) Or KeyCode Like vbKeyNumpad & "*" Then
    If Len(Me.machbox.Value) > 3 Then nlist 'prevents the query from firing when the filter is too broad
End If
End Sub
Sub nlist

'Set F as the filter, which is the value of the listbox
F = machbox.Value

'I have ommited the code that connects to the DB

'Clear the listbox so it won't keep the records from the previous filter
Me.ListBox1.Clear

'Execute the query and parse the records to the listbox
Set rs = cnn.Execute("Select * From Table Where Code = '" & F & "'") 'example query
If (rs.RecordCount <> 0) Then
   Do While Not rs.EOF
      Me.ListBox1.AddItem (rs.Fields(0).Value)
      rs.MoveNext
   Loop
End If

End Sub
子列表
'将F设置为筛选器,它是列表框的值
F=马赫箱值
'我已输入连接到数据库的代码
'清除列表框,使其不会保留上一个筛选器中的记录
Me.ListBox1.Clear
'执行查询并将记录解析到列表框
设置rs=cnn.Execute(“Select*From Table Where Code=”&F&“”)示例查询
如果(rs.RecordCount 0),则
做而不做
Me.ListBox1.AddItem(rs.Fields(0).Value)
下一个
环
如果结束
端接头

您能提供一些您尝试使用的代码以及您迄今为止尝试的内容吗?在我的项目中,我有一个用户表单,其中包含一个文本框和一个列表框(后者也可以是一个组合框)。我对文本框使用了一个,以查看用户是否在框中键入了任何内容,然后根据文本框中的筛选器重新运行填充其下方组合框的sql查询。也许这可以将您设置为正确的方向?我还尝试使用组合框,通过使用excel公式更改组合框的属性。但它不起作用@Timstack事实上我不知道代码的作者,所以我不知道共享代码是否是一个好主意,尽管我已经在帖子中添加了代码。@Rohrl77谢谢你的帮助。但我仍在试图找出你的代码。在我的情况下,下拉列表中选择的值充当变量,并包含在sql查询中。并且我们不是只使用一个组合框,选择动态范围和更新下拉列表的代码以及下拉列表的建议。我不知道我对你是否有任何意义。但我的头脑有点僵硬。@timstack我不太明白。我的理解是,您希望使用一个下拉框,它只能根据您键入的内容显示值。这就是我所构建的,只是我为过滤器创建了一个单独的文本框,列表框中显示的选项直接来自数据库,因为“数据是从数据库中提取的,因此范围必须是动态的,应该使用VBA在下拉列表中自动更新”。我已将解释性评论编辑到我的代码中感谢@TimStack.:)的解释没问题!如果这有助于解决您的问题,请不要忘记接受答案。