ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Listbox with dynamic strings (https://www.excelbanter.com/excel-programming/455039-listbox-dynamic-strings.html)

Tatsujin

Listbox with dynamic strings
 
I have a Userform that contains one TextBox and one ListBox. The ListBox will initially contain about 300 unique string values.

If a user types a short substring into the TextBox, I would like to only visually display the lines that contain that substring. Therefore, the items in the ListBox will dynamically change, depending on the substring entered in the TextBox.

What would be an efficient way to implement this? Would I have to store the strings in an array and constantly update both the array and ListBox?

Thanks!

Peter T[_8_]

Listbox with dynamic strings
 
"Tatsujin" wrote in message
I have a Userform that contains one TextBox and one ListBox. The ListBox
will initially contain about 300 unique string values.

If a user types a short substring into the TextBox, I would like to only
visually display the lines that contain that substring. Therefore, the
items in the ListBox will dynamically change, depending on the substring
entered in the TextBox.

What would be an efficient way to implement this? Would I have to store
the strings in an array and constantly update both the array and ListBox?


One of several ways, in a light test this worked for me

With sample text in A1:A300, a form with a listbox and textbox, type in the
textbox

Private Sub TextBox1_Change()
Dim i As Long
Dim sMatch As String
Dim arr
arr = Range("A1:A300").Value
sMatch = LCase$("*" & Me.TextBox1 & "*")
With Me.ListBox1
.Clear
For i = 1 To UBound(arr)
If LCase(arr(i, 1)) Like sMatch Then
.AddItem arr(i, 1)
End If
Next
End With
End Sub

Peter T




All times are GMT +1. The time now is 05:45 PM.

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