简体   繁体   中英

Items not adding to combobox VBA

I am using Excel VBA to place strings from another sheet's row of cells into a combobox dropdown. When a user types into the combobox, the dropdown should be filtering results to only results that contain the same character(s) as typed in the combobox. However, I cannot get the code to make this behavior happen. The code only seems to grab the first character from each string in the data sheet and not match a character at any position of any string.

When the workbook is open:

Public Sub Workbook_Open()
InitnewCmb
End Sub

newMdl:

This part all works:

Public newCol As Collection
Public indNewCol As Long
Public lastColumn As Long

Public newCargoNum As Long



Public Sub InitnewCmb()
'Initialize combobox
lastColumn = Database.Cells.SpecialCells(xlCellTypeLastCell).Column

    Set newCol = New Collection
    newCargoNum = 0
    With newCol
        For indNewCol = 2 To lastColumn

            .Add Database.Cells(2, indNewCol).Value
        'Take the value of each cell, a string and add to the collection of strings

        newCargoNum = newCargoNum + 1

        Next indNewCol

    End With

Here's where things get out of hand. FilternewCmb is called now inside InitnewCmb

FilternewCmb ""


End Sub

Public Sub FilternewCmb(newFilter As String)
Dim l As Long




    For l = 1 To newCargoNum


        If InStr(1, newCol.Item(l), newFilter, vbTextCompare) <> 0 Then
            'If entered character matches a character in any string in collection
            Tool.newCmb.AddItem newCol.Item(l)


            'keep these strings in dropdown
        End If

    Next l

End Sub

Can someone please point me in the right direction on why the filtering is not working? Finally, once an item is selected in the dropdown, I want that item to populate the combobox and have the dropdown disappear too, which should be easy.

Thank you.

This setup will filter a ComboBox drop-down list as you type

Create a new ComboBox on Sheet1 (ActiveX Control), as in the image bellow, named "ComboBox1"

Add this code to Sheet1 VBA module:

Option Explicit

Private cLst As Variant

Private Sub Worksheet_SelectionChange1(ByVal Target As Range)
    cLst = Sheet1.UsedRange.Columns(1)
    Sheet1.ComboBox1.List = cLst
    Sheet1.ComboBox1.ListIndex = -1
End Sub

Private Sub ComboBox1_Change()
    filterComboList Sheet1.ComboBox1, cLst
End Sub

Private Sub ComboBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Sheet1.ComboBox1.DropDown
End Sub

Private Sub ComboBox1_GotFocus()   'or _MouseDown()
    Sheet1.ComboBox1.DropDown
End Sub

Public Sub filterComboList(ByRef cmb As ComboBox, ByRef dLst As Variant)
    Dim itm As Variant, lst As String, sel As String

    Application.EnableEvents = False
    With cmb
        sel = .Value
        If IsEmpty(cLst) Then cLst = Sheet1.UsedRange.Columns(1)
        For Each itm In cLst
            If Len(itm) > 0 Then If InStr(1, itm, sel, 1) Then lst = lst & itm & "||"
        Next
        If Len(lst) > 0 Then .List = Split(Left(lst, Len(lst) - 2), "||") Else .List = dLst
    End With
    Application.EnableEvents = True
End Sub

Sheet1设定

Change cell selection on Sheet1 to refresh the drop-down list

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