让列表只显示与用户输入的文本匹配的值,这是一场噩梦。以下是我写的有效的(但花了我一段时间!)
请注意,组合框的 MacthEntry 属性必须设置为“2 - frmMatchEntryNone”才能使代码正常工作。 (其他值会导致组合框 .value 属性存储与用户键入的内容匹配的第一个值的文本,并且代码依赖于它来存储他们键入的内容。)
另请注意,绕过您观察到的行为的技巧,即组合框列表的值大小不正确,是使用代码行:
LastActiveCell.Activate
ComboBox_SiteName.Activate
此外,代码将选择列表中包含用户在其文本中任意位置键入的字母的任何项目。
无论如何,这是我的代码:
Private Sub ComboBox_SiteName_GotFocus()
' When it first gets the focus ALWAYS refresh the list
' taking into acocunt what has been typed so far by the user
RePopulateList FilterString:=Me.ComboBox_SiteName.Value
Me.ComboBox_SiteName.DropDown
End Sub
' #4 Private Sub ComboBox_SiteName_Change()
Private Sub ComboBox_SiteName_Enter()
Dim LastActiveCell As Range
On Error GoTo err_Handler
Set LastActiveCell = ActiveCell
Application.ScreenUpdating = False
With Me.ComboBox_SiteName
If .Value = "" Then
' Used cleared the combo
' Repopulate will all values
RePopulateList
.DropDown
Else
' #4 reducdant
' LastActiveCell.Select
' .Activate
' ===========================================
' #4 new code
' CheckBox1 is another control on the form
' which can receive the focus and loose it without event firing
CheckBox1.SetFocus
' This will trigger the GotFocus event handler
' which will do a refresnh of the list
.SetFocus
' ===========================================
End If
End With
Application.ScreenUpdating = True
Exit Sub
err_Handler:
Application.ScreenUpdating = True
Err.Raise Err.Number, "", Err.Description
Exit Sub
Resume
End Sub
Private Sub RePopulateList(Optional FilterString As String = "")
Dim i As Long
Dim ValidValues() As Variant
' #2 range now refers to just the data cells
ValidValues = Worksheets("Address").Range("Table5[SITE NAME]").Value
With Me.ComboBox_SiteName
If FilterString = "" Then
' All all values
.List = ValidValues
Else
' #2: .List cannot be set to have no items.
' so remove all but one
.List = Array("Dummy Value")
' Only add values that match the FilterString parameter
For i = LBound(ValidValues, 1) To UBound(ValidValues, 1)
If LCase(ValidValues(i, 1)) Like "*" & LCase(FilterString) & "*" Then
.AddItem ValidValues(i, 1)
End If
Next i
' #2 add this line to remove the dummy item
.RemoveItem (0)
End If
End With
End Sub
Private Sub ComboBox_SiteName_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Application.ScreenUpdating = False
End Sub
================================================ ========================
您可以:将您的所有代码替换为应该提供可接受功能的代码(只要数据源是 alpha 顺序),这很容易!但是,它并不能完全满足您的要求。
Private Sub ComboBox_SiteName_GotFocus()
With Me.ComboBox_SiteName
.List = Worksheets("Address").Range("Table5[[#All],[SITE NAME]]").Value
End With
ComboBox_SiteName.DropDown
End Sub
可以将组合框设置为“根据用户类型进行过滤” - 只要数据按字母顺序排列即可。
================================================ ========================
请注意,在您的代码中,以下两行会导致 ComboBox_SiteName_Change 事件重新开始。我怀疑您需要添加断点并调试更多代码。
.Value = UCase(.Value)
.Clear ' Clear all items
无论如何,我希望这已经完成了。
如果我得到它,这将是我的第一个赏金,所以如果你需要更多帮助,请告诉我。 (我觉得可能值50分以上)
哈维
================================================ =
第 2 部分:
回答您的评论问题:
(见上面我代码中的#2标签)
要引用表格列的数据,不包括标题使用:
=Table5[站点名称]
(如果您单击并拖动列中的数据单元格,这将在输入公式时自动生成)。
代码已相应更改。
我使用了 excel 2013 和 2010,发现 .Activate 事件在两者中都有效。
请参阅 #3 进行细微更改。
请重新复制所有代码。
请注意,我引入了代码来尝试使用 Application.ScreenUpdating 停止闪烁,但它没有任何效果 - 我不知道为什么。我已将代码留在其中,因此您可以根据需要进行进一步的实验。
注意新过程 ComboBox_SiteName_KeyDown
================================================ =
第 3 部分:
回答您的评论问题:
这是表格上的组合! - 所以进行上面标有 #4 的更改。
哈维