简体   繁体   中英

Excel crash when trying to Autocomplete an ActiveX ComboBox

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM