1

How to make suggestions in Excel data validation list while typing. There are constraints in my request:

  1. The list of items should be in another sheet, and must not be above in hidden rows.
  2. Typing a phrase should narrow the list to all the items which contain the phrase.
  3. Search should be case insensitive.

So after typing am we should hypothetically have a suggestion to pick up from Amelia, Camila, Samantha, provided that those girls' names are on the item list.

I have found a good solution here, however it does not filter the items with contains clause but begins with. I sum up the proposed solution here shortly.

  1. We insert a Combo Box (ActiveX Control) to a sheet.
  2. We right click on a sheet name > View code > and paste the VBA code in the sheet VBA editor:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Update by Extendoffice: 2018/9/21
        Dim xCombox As OLEObject
        Dim xStr As String
        Dim xWs As Worksheet
        Dim xArr
        Set xWs = Application.ActiveSheet
        On Error Resume Next
        Set xCombox = xWs.OLEObjects("TempCombo")
        With xCombox
            .ListFillRange = ""
            .LinkedCell = ""
            .Visible = False
        End With
        If Target.Validation.Type = 3 Then
            Target.Validation.InCellDropdown = False
            Cancel = True
            xStr = Target.Validation.Formula1
            xStr = Right(xStr, Len(xStr) - 1)
            If xStr = "" Then Exit Sub
            With xCombox
                .Visible = True
                .Left = Target.Left
                .Top = Target.Top
                .Width = Target.Width + 5
                .Height = Target.Height + 5
                .ListFillRange = xStr
                If .ListFillRange = "" Then
                    xArr = Split(xStr, ",")
                    Me.TempCombo.List = xArr
                End If
                .LinkedCell = Target.Address
            End With
            xCombox.Activate
            Me.TempCombo.DropDown
        End If
    End Sub
    
    Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        Select Case KeyCode
            Case 9
                Application.ActiveCell.Offset(0, 1).Activate
            Case 13
                Application.ActiveCell.Offset(1, 0).Activate
        End Select
    End Sub
    

I could not find a way to modify to change the search option from 'begins with' to contains.

The questions about autocomplete or autosuggest in validation list have been asked so far.
Excel data validation with suggestions/autocomplete
Excel 2010: how to use autocomplete in validation list
But neither of them contained answers which would satisfied the constraints I imposed.

Test file for download is here.

Przemyslaw Remin
  • 6,276
  • 25
  • 113
  • 191
  • In general both, the data validation and the ComboBox don't support what you ask for. The only idea I have is to remove all items from `xArr` in a loop that don't contain the typed string eg `am`. And refresh the ComboBox list on every `TempCombo_KeyDown`. But if the list is long this could easily get very slow. – Pᴇʜ Jan 11 '19 at 10:19
  • @PEH can you please put it as an answer. Which variable holds the currently typed phrase? – Przemyslaw Remin Jan 11 '19 at 11:42

2 Answers2

2

Try to add the following event (additionally the the other 2). Every time you enter something the code refreshes the ComboBox list.

Private Sub TempCombo_Change()
    With Me.TempCombo
        If Not .Visible Then Exit Sub
        .Clear 'needs property MatchEntry set to 2 - fmMatchEntryNone
        .Visible = False 'to refresh the drop down
        .Visible = True
        .Activate
        Dim xStr As String, xArr As Variant
        xStr = TempCombo.TopLeftCell.Validation.Formula1
        xStr = Right(xStr, Len(xStr) - 1)
        xArr = Split(xStr, Application.International(xlListSeparator))
        Dim itm As Variant
        For Each itm In xArr
            If InStr(1, itm, .Value, vbTextCompare) > 0 Or .Value = "" Then
                .AddItem itm
            End If
        Next itm
        .DropDown
    End With
End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • Pops up error in the line: .List = Array() with message `Permission denied`. What is Array() ? It is empty. – Przemyslaw Remin Jan 11 '19 at 13:41
  • @PrzemyslawRemin Cannot reproduce it. I created a new workbook, pulled in your macros, wrote my macro, added a new ActiveX ComboBox and named it `TempCombo` and added a data validation list to the cells. That's all and it worked. – Pᴇʜ Jan 11 '19 at 13:46
  • What is Array()? is it a variable? Or anything that stores data? What it refers? Is it supposed to store any data before the event fires? – Przemyslaw Remin Jan 11 '19 at 13:50
  • @PrzemyslawRemin `Array()` is an empty array to reset the list. – Pᴇʜ Jan 11 '19 at 13:51
  • Can it be declared before? Something like `dim Array()` – Przemyslaw Remin Jan 11 '19 at 13:55
  • @PrzemyslawRemin no [`Array()` is a VBA function](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/array-function). As I said already the code works in my test file. There is nothing wrong with it. – Pᴇʜ Jan 11 '19 at 13:57
  • Is this line `.List = Array()` supposed to remove all items from the dropdown list of combobox? – Przemyslaw Remin Jan 11 '19 at 14:12
  • @PrzemyslawRemin replace it with `.Clear`. But then it is necessary to set ComboBox property `MatchEntry` to `2 - fmMatchEntryNone`. For case insensitivity use `vbTextCompare` in `InStr(1, itm, .Value, vbTextCompare)` Updated my Answer. – Pᴇʜ Jan 11 '19 at 14:18
  • Still no success. I have updated my answer adding a link to a test file for download (at the bottom of the answer). – Przemyslaw Remin Jan 11 '19 at 14:43
  • Meh sorry I don't download macro enabled files from unknown sources (there is a reason why you cannot upload files here). Instead please do the following: Create a new workbook, pull in your 2 macros, pull in my macro, add a new ActiveX ComboBox and name it `TempCombo` and add a data validation list to the cells eg in column A. This worked for me. – Pᴇʜ Jan 11 '19 at 15:35
  • @PEH I run into Object does not support this property of method. Do you have any reference enabled? I wonder what is wrong. Because the first VBA code, the one in my answer runs smoothly without any prompt. In your sub I think there is a problem with reference to the object. It is somehow not recognized. – Przemyslaw Remin Jan 14 '19 at 09:44
  • @PrzemyslawRemin In which line do you get this error? • No references set here. Make sure your ComboBox is a ActiveX ComboBox and not a FormControl ComboBox. – Pᴇʜ Jan 14 '19 at 09:54
  • The line `.Clear` . I am sure it is ActiveX. Can you upload your file? I would take the risk:-) – Przemyslaw Remin Jan 14 '19 at 10:37
  • 1
    I got the worm. Your code works ok. However if you change the source of data validation list problems starts. Your list was defined as `Amelia,Camila,Samantha` if you change it to `=items!$A$1:$A$41` (items being a sheet name) then you will experience all weird behavior I faced. – Przemyslaw Remin Jan 14 '19 at 11:11
  • So I assume this answers the original question and we can mark it as solution? – Pᴇʜ Jan 14 '19 at 12:41
  • The constrain no 1 in my original question is not satisfied. Would you mind helping on that? I certainly will mark it as solution nevertheless. – Przemyslaw Remin Jan 14 '19 at 13:09
  • @PrzemyslawRemin The issue is that it will change the question completely and probably totally mess it up. Better to close here and ask a new question on how to use a range instead of a comma separated list. Would be cleaner and other people can answer too. – Pᴇʜ Jan 14 '19 at 13:14
  • I am very grateful. Thank you:-) – Przemyslaw Remin Jan 15 '19 at 08:44
  • I couldn't use this code. It keeps giving an exception on ".Clear" even if I change my MatchEntry configuration. I've also tried to comment that, but without success. – Iúri dos Anjos Jul 29 '19 at 18:47
  • @IúridosAnjos Please ask your own/new question showing the excact code you use. We cannot answer your question here. – Pᴇʜ Jul 30 '19 at 05:57
  • @Pᴇʜ I noticed that The .Visible = False is firing the Lost_Focus event, how to avoid that and on the same time if i don't do .Visible=False/true the control doesn't refresh – JustGreat Nov 14 '22 at 18:49
0

To overcome your first constraint, maybe you can assign a range to your combo box:

Dim xCombox             As OLEObject
    Dim xStr                As String
    Dim xWs                 As Worksheet
    Dim xArr
    Dim i                   As Range

    Set xWs = Application.ActiveSheet
    On Error Resume Next
    Set xCombox = xWs.OLEObjects("Combotest")
    With Sheets("Test_list2")
    Set i = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
    End With
    Combotest.ListFillRange = i.Address
 Set xWs = Application.ActiveSheet
    On Error Resume Next
    Set xCombox = xWs.OLEObjects("Combotest")
    With xCombox
        .LinkedCell = "F2"
        .Visible = True
    End With
.
.
.
.
End Sub
P. Mehta
  • 11
  • 5