Excelcombobox更新下拉菜单?

有没有办法刷新combobox? 我有以下的VBA代码。 填充下拉列表,直到列表被清除并填充了匹配项的If语句。

此时,下拉列表只显示带滚动条的单个项目。 但是,如果我closures了下拉菜单并重新打开,则它将正确填充。

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中input时,ComboBox_Change函数被调用..下拉框从Clear / Repopulate匹配的项目之后,用一个向上/向下箭头从一个列表变成一行。但是如果我closures下拉部分并重新打开列出没有向上/向下箭头的所有项目。 .ListRows值= 8的方式。

我想要一个方法的下拉药水要么closures和重新打开..或VBAfunction刷新下拉部分,没有外部button或控件

让列表只显示与用户input的文字相匹配的值是一场噩梦。 下面是我写的那个作品(但是花了我一会儿!)

请注意,combobox的MacthEntry属性务必设置为“2 – frmMatchEntryNone”以使代码正常工作。 (其他值会导致combobox.value属性存储与用户键入的值相匹配的第一个值的文本,并且代码依赖于存储它们键入的内容)。

另外请注意,解决您观察到的行为的技巧,即combobox列表中的值的大小不正确,就是使用代码行:

 LastActiveCell.Activate ComboBox_SiteName.Activate 

此外,该代码将获取列表中的任何项目,其中包含用户ANYWHERE在其文本中键入的字母。

无论如何,这是我的代码:

 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 

================================================== ====================

你可以:用你的代码replace掉所有可以接受的function(只要数据源是按照字母顺序),这很容易! 但是,这并不完全符合你的要求。

 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设置为“按用户types过滤” – 只要数据按字母顺序排列即可。

================================================== ====================

请注意在您的代码中以下两行导致ComboBox_SiteName_Change事件再次启动。 我怀疑你需要添加断点和debugging你的代码更多。

 .Value = UCase(.Value) .Clear ' Clear all items 

无论如何,我希望这是工作完成。

这将是我的第一个赏金,所以请让我知道如果你需要任何帮助。 (我认为可能会超过50分)

哈维

================================================

第2部分:

回答你的评论问题:

(请参阅上面我的代码中的#2标记)

要引用表格列的数据,不包括标题use:= Table5 [SITE NAME](如果单击并拖动列中的数据单元格,将在input公式时自动生成)。 代码已经被改变了。

我用Excel 2013和2010,发现.Activate事件在两个工作。 请参阅#3进行小的更改。

请重新复制所有的代码。

请注意,我介绍的代码尝试使用Application.ScreenUpdating停止闪烁,但它没有任何效果 – 我不知道为什么。 我已经把代码留下了,所以你可以做进一步的实验,如果你需要的话。

注意新程序ComboBox_SiteName_KeyDown

================================================

第3部分:

为了回答你的评论问题:这是一个表单上的组合! – 所以做上面#4标记的变化。

哈维