Excel crash when trying to Autocomplete an ActiveX ComboBox

萝らか妹 提交于 2020-04-07 02:52:21

问题


I have an ActiveX Combobox control on a worksheet and this is the _Change event code

Private Sub ComboBox1_Change()
  Me.ComboBox1.ListFillRange = "ItemSearch"
  Me.ComboBox1.DropDown
End Sub

When I use keyboard up/down key to move through the list it automatically quits Excel.

Does anyone know the solution to this problem? I basically want a dynamic ComboBox.


回答1:


You are forcing Combobox's possible values to update when you change the selected option,
that is why it's crashing.

You can try to keep Me.ComboBox1.DropDown in that event.

But the .ListFillRange should be in another event :

  • Workbook_Open
  • Workbook_SheetChange
  • Worksheet_SelectionChange
  • Worksheet_Change

If you're attempting to have a kind of AutoComplete behavior, you can use a built-in property :

  1. Right-click on the Control, click on Properties
  2. In the opened Properties window, find the MatchEntry property
  3. Set it to 0 - fmMatchEntryFirstLetter



回答2:


I have solved this by setting ListFillRange only if no item is selected, ie

 If Me.TempCombo.ListIndex = -1 Then
    Me.TempCombo.ListFillRange =...          
 End If

This solves the error and is desirable as (at least in my case) range can only have 1 item when something is selected. So, user types for autocomplete, but when he starts using arrows he can choose from filtered so far values.

Complete example is available there . Unlike in this often mentioned example as user types it filters out entries which don't match current input



来源:https://stackoverflow.com/questions/42162379/excel-crash-when-trying-to-autocomplete-an-activex-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!