Search code examples
excelvbauserform

Handling a smart Combobox in a userform


I found this smart code that lists the hits while you type the name in, like a quick search. Everything works fine, but if i click on the arrow next to the combobox and select something from the list, I have to press enter before it triggers. I want it to run my "viewProject" when clicked. Secondly and a bonus is that if the list on the active sheet contains less than 3 objects, the code gets error. I did a quick fix, making a space if that would accure. Is there a better way? Thanks for your help!

Option Explicit

Private Comb_Arrow As Boolean

Private Sub ComboBox1_Change()
Dim i As Long
If ActiveSheet.Cells(2, 2) = "" Then ActiveSheet.Cells(2, 2) = " "
If ActiveSheet.Cells(3, 2) = "" Then ActiveSheet.Cells(3, 2) = " "
If Not Comb_Arrow Then
    With ComboBox1
        .List = ActiveSheet.Range("B2", Cells(Rows.count, 2).End(xlUp)).value
        .ListRows = Application.WorksheetFunction.Min(4, .ListCount)
        .DropDown
        If Len(.text) Then
            For i = .ListCount - 1 To 0 Step -1
                If InStr(1, .List(i), .text, vbTextCompare) = 0 Then .RemoveItem i
            Next
            .DropDown
        End If
    End With
End If
End Sub

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Comb_Arrow = (KeyCode = vbKeyUp) Or (KeyCode = vbKeyDown)
If KeyCode = vbKeyReturn Then
    With ComboBox1
        If .ListIndex = -1 And .LineCount > 0 Then
            .ListIndex = 0
        End If
        .List = ActiveSheet.Range("B2", Cells(Rows.count, 2).End(xlUp)).value
        If .ListIndex > -1 Then
            Call viewProject
        End If
    End With
End If
End Sub

Solution

  • You might have found the solution yourself by now but for future readers (and possible you still), here goes:

    The issue with your .List throwing an error was not 3 but 2 rows (since you start looking from B2) and if you have B3 empty, only one value is given to .List while it expects an array. Range("B2").Value = just the value while Range("B2:B3").Value is an array.

    As for the click, you can normally just use the Click event for that:

    Private Sub ComboBox1_Change()
        Dim i As Long
        Dim rng As Range: Set rng = ActiveSheet.Range("B2", Cells(Rows.Count, 2).End(xlUp))
        If rng.Count < 2 Then Set rng = rng.Resize(2) 'get the empty cell beneath it as well
        If Not Comb_Arrow Then
            With ComboBox1
                .List = rng.Value
                .ListRows = Application.WorksheetFunction.Min(4, .ListCount)
                .DropDown
                If Len(.Text) Then
                    For i = .ListCount - 1 To 0 Step -1
                        If InStr(1, .List(i), .Text, vbTextCompare) = 0 Then .RemoveItem i
                    Next
                    .DropDown
                End If
            End With
        End If
    End Sub
    
    Private Sub ComboBox1_Click()
        viewProject 'this should only run your macro when selecting a list item
    End Sub