部分字符串匹配(在组合框中键入时如何匹配字符串的任何部分?)
Partial String Match (How to match any part of a string while I type in a combobox?)
我有:
[表格:"Intajform"] - [组合框:"CustomerName_Combobox"] - [初始化事件:加载列表]
Private Sub UserForm_Initialize()
Dim ws As Worksheet, rCell, srr As Range, Key
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Set ws = ThisWorkbook.Worksheets("Backend")
Set srr = ws.Range("b2", ws.Cells(Rows.Count, "b").End(xlUp))
For Each rCell In srr
If Not Dic.exists(rCell.Value) Then
Dic.Add (rCell.Value), Nothing
End If
Next rCell
For Each Key In Dic
IntajForm.CustomerName_Combobox.AddItem Key
Next
End Sub
在该组合框中键入以查找匹配项时
我只能通过第一个字母完全匹配字符串
例如:如果我输入 "M" 然后我找到 "Microsoft Corporation"
但我无法部分匹配字符串
例如:如果我输入 "r" 那么它是空白的 " "
目标
我想部分匹配 Combobox 中找到的字符串,只要在字符串中找到我键入的字母即可。
使用组合框的 _Change()
事件
您只能减少定义组合框的 .MatchEntry
属性 的可能性 - 参见。 MS Help - MatchEntry property:
.MatchEntry = fmMatchEntryFirstLetter
'
.MatchEntry = fmMatchEntryComplete
'
.MatchEntry = fmMatchEntryNone
'根本没有自动预选
要在当前输入的部分字符串之后获得一个缩小的选择列表,您必须使用组合框的 _Change()
事件并将字典声明移动到模块头以使其对 init 和 combo 都可用变化。
我们的想法是在组合框 [1] 中的任何条目之后呈现个性化选择,并强制下拉以获得更好的概览 [2]:
Option Explicit ' declaration head of code module
Dim Dic As Object ' make dictionary available at module level
Private Sub CustomerName_Combobox_Change()
With Me.CustomerName_Combobox
'[1] narrow data to choose
.List = Filter(Dic.Keys, .Text, True, vbTextCompare) ' <~ corrected/2020-03-15
'[2] expand filter selection (reduced number of valid elements)
.DropDown
End With
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet, rCell As Range, srr As Range, Key
Set Dic = CreateObject("Scripting.Dictionary")
Set ws = ThisWorkbook.Worksheets("Backend")
Set srr = ws.Range("b2", ws.Cells(Rows.Count, "b").End(xlUp))
For Each rCell In srr
If Not Dic.exists(rCell.Value) Then
Dic.Add (rCell.Value), Nothing
End If
Next rCell
Me.CustomerName_Combobox.List = Dic.Keys ' << prefix the Me particle to indicate the current instance, not the Userform name itself :-)
End Sub
进一步提示: 可以一次性将所有字典键分配给组合框 .List
属性 而不是循环(参见 _Initialize()
).并避免像 IntajForm.CustomerName_Combobox.AddItem Key
这样的结构,只需引用控件的名称或在其前面加上 Me
以指示用户窗体 (class) 的当前实例,从而为您提供 public 成员目的。
仅供参考 - 要进一步了解 Me
限定符和用户表单本身,您可能会从阅读
中获益
我有:
[表格:"Intajform"] - [组合框:"CustomerName_Combobox"] - [初始化事件:加载列表]
Private Sub UserForm_Initialize()
Dim ws As Worksheet, rCell, srr As Range, Key
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Set ws = ThisWorkbook.Worksheets("Backend")
Set srr = ws.Range("b2", ws.Cells(Rows.Count, "b").End(xlUp))
For Each rCell In srr
If Not Dic.exists(rCell.Value) Then
Dic.Add (rCell.Value), Nothing
End If
Next rCell
For Each Key In Dic
IntajForm.CustomerName_Combobox.AddItem Key
Next
End Sub
在该组合框中键入以查找匹配项时
我只能通过第一个字母完全匹配字符串 例如:如果我输入 "M" 然后我找到 "Microsoft Corporation"
但我无法部分匹配字符串 例如:如果我输入 "r" 那么它是空白的 " "
目标
我想部分匹配 Combobox 中找到的字符串,只要在字符串中找到我键入的字母即可。
使用组合框的 _Change()
事件
您只能减少定义组合框的 .MatchEntry
属性 的可能性 - 参见。 MS Help - MatchEntry property:
.MatchEntry = fmMatchEntryFirstLetter
'.MatchEntry = fmMatchEntryComplete
'.MatchEntry = fmMatchEntryNone
'根本没有自动预选
要在当前输入的部分字符串之后获得一个缩小的选择列表,您必须使用组合框的 _Change()
事件并将字典声明移动到模块头以使其对 init 和 combo 都可用变化。
我们的想法是在组合框 [1] 中的任何条目之后呈现个性化选择,并强制下拉以获得更好的概览 [2]:
Option Explicit ' declaration head of code module
Dim Dic As Object ' make dictionary available at module level
Private Sub CustomerName_Combobox_Change()
With Me.CustomerName_Combobox
'[1] narrow data to choose
.List = Filter(Dic.Keys, .Text, True, vbTextCompare) ' <~ corrected/2020-03-15
'[2] expand filter selection (reduced number of valid elements)
.DropDown
End With
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet, rCell As Range, srr As Range, Key
Set Dic = CreateObject("Scripting.Dictionary")
Set ws = ThisWorkbook.Worksheets("Backend")
Set srr = ws.Range("b2", ws.Cells(Rows.Count, "b").End(xlUp))
For Each rCell In srr
If Not Dic.exists(rCell.Value) Then
Dic.Add (rCell.Value), Nothing
End If
Next rCell
Me.CustomerName_Combobox.List = Dic.Keys ' << prefix the Me particle to indicate the current instance, not the Userform name itself :-)
End Sub
进一步提示: 可以一次性将所有字典键分配给组合框 .List
属性 而不是循环(参见 _Initialize()
).并避免像 IntajForm.CustomerName_Combobox.AddItem Key
这样的结构,只需引用控件的名称或在其前面加上 Me
以指示用户窗体 (class) 的当前实例,从而为您提供 public 成员目的。
仅供参考 - 要进一步了解 Me
限定符和用户表单本身,您可能会从阅读