ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Highlight multiple items in listbox based on textbox item (https://www.excelbanter.com/excel-programming/448825-highlight-multiple-items-listbox-based-textbox-item.html)

Gizzmo

Highlight multiple items in listbox based on textbox item
 
I have a user form with a ListBox2 (which represents a list for 'facility' code and description) that is populated when the form is Initialized.

I also have a text box that represents the same 'facility' that is populated when the user selects an item from a second ListBox1 (which looks at existing records on a spreadsheet and brings back records based on a series of combo box selections made by the user).

What I would like to be able to do is once the tex box has been populated by selecting ListBox1, I would like for the items (NB: there can be multiple items in the text box i.e. I have a string of text in the Facility code on the spreadsheet seperated by a carriage return in the cell) that are now in the text box to be highlighted in List Box 2!

At the moment I have code that is able to populate the text box once the user highlights multiple items in List Box 2 and exits, but I want to able to do the revers i.e. have the items become highlighted in List Box 2 (before the user has taken any action) based on the items in the text box.

Can any one help?

Gizzmo

I have solved my problem thanks.

If you are interested here is snippet of the code

[code]
.txtFacility.Value = ListBox1.List(r, 8)

Dim var() As String
Dim x As Integer
Dim y As Integer
Dim st As String

st = ListBox1.List(r, 8)

x = InStr(1, st, Chr(13))
'x = InStr(1, st, ",")
var = Split(st, Chr(13))
'var = Split(st, ",")

For x = 0 To UBound(var)
For y = 0 To Me.ListBox2.ListCount - 1
'If Me.ListBox2.ListIndex(y) = var(x) Then
' If InStr(1, UCase(Trim(Me.ListBox2.List(y, 0))), UCase(Trim(var(x))), vbTextCompare) 0 Then
If StrComp(Me.ListBox2.List(y, 0), var(x), vbTextCompare) = 0 Then

'If UCase(Trim(Me.ListBox2.List(y, 0))) = UCase(Trim(var(x))) Then
Debug.Print UCase(Trim(Me.ListBox2.List(y, 0)))
Debug.Print UCase(Trim(var(x)))
Me.ListBox2.Selected(y) = True

Exit For

End If
Next

Next
[code]


All times are GMT +1. The time now is 03:09 AM.

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