有没有办法刷新组合框? 我有以下 VBA 代码。下拉列表将被填充,直到 If 语句中的列表被清除并填充匹配的项目。
此时,下拉列表仅显示带有滚动条的单个项目。但是如果我关闭下拉菜单并重新打开,它就会完全正确填充。
Private Sub ComboBox_SiteName_Change()
ComboBox_SiteName.DropDown
Dim v As Variant, i As Long
With Me.ComboBox_SiteName
.Value = UCase(.Value)
If .Value <> "" And .ListIndex = -1 Then
v = Worksheets("Address").Range("Table5[[#All],[SITE NAME]]").Value
.Clear ' Clear all items
' Repopulate with matched items
For i = LBound(v, 1) To UBound(v, 1)
If LCase(v(i, 1)) Like "*" & LCase(.Value) & "*" Then
.AddItem v(i, 1)
End If
Next i
Else
' Repopulate with all items
.List = Worksheets("Address").Range("Table5[[#All],[SITE NAME]]").Value
End If
End With
End Sub
当用户在组合框中键入内容时,将调用 ComboBox_Change 函数。在清除和重新填充匹配项后,下拉框从列表变成带有向上/向下箭头的单行。 但如果我关闭下拉部分并重新打开,它会列出所有没有向上/向下箭头的项目。 顺便说一句,.ListRows 值 = 8。
我想要一种方法让下拉菜单关闭并重新打开..或者使用 VBA 函数来刷新下拉部分,请无需外部按钮或控件
最佳答案
让列表仅显示与用户迄今为止输入的文本相匹配的值,这是一场噩梦。以下是我写的有效内容(但花了我一段时间!)
请注意,组合框的 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 标记)
要引用表列的数据(不包括标题),请使用: =表5[站点名称] (如果您单击并拖动列中的数据单元格,则输入公式时将自动生成)。 代码已相应更改。
我使用了 Excel 2013 和 2010,发现 .Activate 事件在两者中都有效。 请参阅#3 进行细微更改。
请重新复制所有代码。
请注意,我引入了代码来尝试使用 Application.ScreenUpdating 停止闪烁,但它没有任何效果 - 我不知道为什么。我已保留代码,以便您可以根据需要进行进一步的实验。
注意新过程 ComboBox_SiteName_KeyDown
================================================== =
第 3 部分:
回答您的评论问题: 这是表格上的组合! - 因此请进行上面标记为 #4 的更改。
哈维
关于Excel 组合框刷新下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31660393/