Stuck in a combobox event

eduzs

Well-known Member
Joined
Jul 6, 2014
Messages
704
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi

I have a CB1 combobox, the problem is:

I want to trigger an event ONLY when I click on a value in the drop-down list.
There's a FilterTheComboBox sub that filter as I type in the dropdown list.

For example, suppose a dropdown list with: "House", "House A", "House B".

1) Using MouseClick event, if I type in CB1 a value that exactly matches a value in the list, the MOUSECLICK event will not work when I click the same value in the drop-down list;

Example: In this scenario, if I type "Home", "Home" is highlighted in the drop-down list, if I click "Home", nothing will happen.
i.e. when CB1 value = dropdownlist value absolutely no mouse click event occurs, no matter how much clicks I do in CB1.

2) Using Change event, when I type a value that exactly matches a value in the list, it calls the sub even when I just want to filter the drop-down list.

Example: Type "House" executes the macro with CB1 = "House" but if I do not want it? For example, I want to select "House A" or "House B"? :(

Code:
Private Sub CB1_Change()

Dim z As Integer

If Me.CB1 <> "" Then
    For z = 0 To UBound(DropDownList)
        If LCase(LCase(Me.CB1)) = LCase(DropDownList(z)) Then Call ModMain.RunMyMacro
    Next z
End If

End Sub

Private Sub CB1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

Call ModControl.FilterTheComboBox

End Sub

Any advice?
Thanks
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Apparently there is no solution to this.
I've found a workaround by my self, I add a double " " blank space in each list item so that the value typed in the combo box never matches a value in the drop down list that will ensure that the click event will always work.
In the Change event I alway remove the extra blank spaces to return the combobox value to the actual value.

For collaboration purposes only, here's some parts of my code

Code:
Private Sub CB1_Change()

With CB1
    If .Value <> "" And .ListIndex <> -1 Then ' If there's a item selected in the dropdown list 
        .Value = Replace(.Value, "  ", "") ' Remove the extra blank spaces
        Call ModMain.RunMySub ' Call the main sub
    End If
End With

End Sub

Private Sub CB1_Keyup(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

If Right(CB1, 1) = " " And KeyCode = 32 Then KeyCode = 0 ' Avoid user to type two blank spaces

If KeyCode = 13 Then ' Call change event if user type enter and there's only one value in the list
    With CB1
        If .ListCount = 1 Then
           .ListIndex = 0
            CB1_Change
        End If
    End With
Else
    Call ModMain.FilterComboBox ' Search and fill the dropdown list with only values that are like the typed text
End If

End Sub

Thanks all
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top