Select in Listbox using keystrokes

S

sbcxp

H

I have a ListBox in a UserForm that holds several elements, like

Englan
Leg
Lisabo
Londo
Madri
Mila
etc..

I would like to be able to select an entry by typing the name in a text-box I have placed above the Listbox. It should highlight the word I type in the listbox

Normally, by using keystrokes in the Listbox, typing "L" will highlight the first word with beginning with L. Typing "E" will highlight the first word beginning with "E". I would like the form to behave in a way so if I type "LE" it highlights the first word beginning with "LE" and so on

Is there a way to do that in a UserForm

//Sore
[email protected]
 
B

Bob Phillips

Hi Soren,

Here's some starter code

Dim colList As Collection

Private Sub TextBox1_Change()
Dim i As Long

With TextBox1
For i = 1 To colList.Count
If LCase(.Text) = LCase(Left(colList.Item(i), Len(.Text))) Then
ListBox1.ListIndex = i - 1
Exit For
End If
Next i
End With

End Sub

Private Sub UserForm_Activate()
Dim col

Set colList = New Collection
With colList
.Add "England", "England"
.Add "Lego", "Lego"
.Add "Lisabon", "Lisabon"
.Add "London", "London"
.Add "Madrid", "Madrid"
.Add "Milan", "Milan"
End With

TextBox1.SetFocus
With ListBox1
For Each col In colList
.AddItem col
Next col
End With

End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

Hi

I have a ListBox in a UserForm that holds several elements, like:

England
Lego
Lisabon
London
Madrid
Milan
etc...

I would like to be able to select an entry by typing the name in a
text-box I have placed above the Listbox. It should highlight the word I
type in the listbox.
Normally, by using keystrokes in the Listbox, typing "L" will highlight
the first word with beginning with L. Typing "E" will highlight the first
word beginning with "E". I would like the form to behave in a way so if I
type "LE" it highlights the first word beginning with "LE" and so on.
 
Top