ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Select in Listbox using keystrokes (https://www.excelbanter.com/excel-programming/297492-select-listbox-using-keystrokes.html)

[email protected]

Select in Listbox using keystrokes
 
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



Bob Phillips[_6_]

Select in Listbox using keystrokes
 
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)

wrote in message
...
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.

Is there a way to do that in a UserForm?

//Soren





[email protected]

Select in Listbox using keystrokes
 
Thanks, it works perfectly :-

//Soren


All times are GMT +1. The time now is 02:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com