Auto-Complete with only text and not numbers ComboBox Excel VBA

前端 未结 2 844
南方客
南方客 2020-12-02 01:30

Suppose I have a ComboBox in Excel VBA with the following sorts of info:

1234 apples
2345 pears
2367 oranges

I want the user to be able to

2条回答
  •  Happy的楠姐
    2020-12-02 01:43

    So first to give some context to this answer. SectorDropDown1_1 (which is part of a form) is a drop down list that is populated with the concatenated strings of a number value and a string value coming from a column J. In this context the _change() method responds to the user typing in values. I have 2 columns "R", and "S", in a spreadsheet "SectorSearch" that have separated the number and the text. So now if the user enters either the number or the text, then the appropriate value in the Drop Down list is selected. But here is the problem, my code sort of awkwardly jumps to the exact value and so I want to "smooth things out" so to speak so that after the user types more than 2 characters that match the appropriate value that this value is now selected and the drop down list shows the nearby values.

    Option Explicit
    
    Private Sub SectorDropDown1_1_Change()
    
    Dim i As Long
    Dim StringRange1 As String
    Dim StringRange2 As String
    Dim Stringrange3 As String
    Dim LengthOfValue As Integer
    Dim TotalSectorCodes As Integer
    
    If SectorDropDown1_1.Value <> "" And Len(SectorDropDown1_1.Value) > 2 Then
        TotalSectorCodes = Worksheets("SectorSearch").Range("J:J").Cells.SpecialCells(xlCellTypeFormulas).Count
        LengthOfValue = Len(SectorDropDown1_1.Value)
        For i = 2 To TotalSectorCodes
            StringRange1 = "R" & CStr(i)
            StringRange2 = "S" & CStr(i)
            Stringrange3 = "J" & CStr(i)
            Select Case SectorDropDown1_1.Value
                Case Left(Worksheets("SectorSearch").Range(StringRange1).Value, LengthOfValue)
                    SectorDropDown1_1.Value = Worksheets("SectorSearch").Range(Stringrange3).Value
                    Exit For
                Case Left(Worksheets("SectorSearch").Range(StringRange2).Value, LengthOfValue)
                    SectorDropDown1_1.Value = Worksheets("SectorSearch").Range(Stringrange3).Value
                    Exit For
                Case Else
                    '...
            End Select
        Next i
    End If
    
    End Sub
    

提交回复
热议问题