Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnKey "Delete" clear combobox

I have a userform with multiple comboboxes. User can type in a new item or pick one from the list.

He can start typing first letters of wanted item but when he makes a mistake and starts with e.g. "b" instead of "n" he has to clear the combobox manually or find the item on the list using mouse or arrows.

I would like to quickly clear the box using the delete key so the user can start typing again. But I don't know where to put this line of code if correct (enter event, change event, some other maybe?).

Application.OnKey "{Delete}", "WyczyscPole"

First Excel needs to know which box the user is in, and then clear it.

I tried to create separate module with a variable that finds out the current combobox name and clear it in next step. But I don't know whether called sub below is even correct.

Sub WyczyscPole()

Dim NazwaPola As ComboBox

NazwaPola = frmZakupy.ActiveControl.Name
NazwaPola.Clear

End Sub
like image 711
kkris77 Avatar asked Dec 05 '25 09:12

kkris77


1 Answers

You can use the KeyDown event to capture the Delete key (KeyCode = 46) when the user is in the combobox.

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

    If KeyCode = 46 Then
        Me.NazwaPola.Value = vbNullString
    End If

End Sub
like image 77
Dick Kusleika Avatar answered Dec 07 '25 22:12

Dick Kusleika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!