Determine the selected item in a multiselect listbox

Michael Simpson

Board Regular
Joined
Jun 21, 2010
Messages
232
I have the following code in my change event for a listbox
Code:
Private Sub Customer_listbox_Change()

Dim counter As Integer

For counter = 1 To Customer_ListBox.ListCount Step 1

    If Me.Customer_ListBox.Selected(counter - 1) = True Then 'selected method has 0 base
        MsgBox ("Selected item")
    Else
      MsgBox ("DESelected item")

    End If
Next counter

End Sub
Now, obviously, all this does is to loop through the items in the listbox and indicate whether they've been selected or not.

What I would like to do is to be able to capture the entry in the listbox that started the change event.
Basically, I've populated the listbox with customers on a sheet. What I would like to do is to change the background colour for the sheet rows based on the items the user selects from the listbox. Assume there are 3 customers. If they select customer 1 from the listbox, then that customer row on the sheet will be coloured green. If they deselect customer 1, the colouring is removed.

I'm assuming my code needs to determine/get the customer's name from the item just (de)selected from the listbox so as to be able to determine which row on the sheet needs to be changed.

Any good suggestions appreciated.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try:

Code:
Private Sub ListBox1_Change()
    Dim i As Long
    With ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                Range(.RowSource).Cells(i + 1, 1).Interior.ColorIndex = 10
            Else
                Range(.RowSource).Cells(i + 1, 1).Interior.ColorIndex = xlNone
            End If
        Next i
    End With
End Sub

The code assumes you have used the RowSource property to populate the ListBox.
 
Upvote 0
As you have Multiselect SET , the code below is a "Lost Focus" event so that it is based on All Selections , Not Changes.
Listfilled range assumed as Column "A".
Code:
Private [COLOR=navy]Sub[/COLOR] ListBox1_LostFocus()
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Dim[/COLOR] c [COLOR=navy]As[/COLOR] [COLOR=navy]Integer,[/COLOR] n [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR]
[COLOR=navy]Dim[/COLOR] Ray()
c = 0
[COLOR=navy]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & rows.Count).End(xlUp))
Rng.EntireRow.Interior.ColorIndex = xlNone
[COLOR=navy]With[/COLOR] ListBox1
    [COLOR=navy]For[/COLOR] n = 0 To .ListCount - 1
        [COLOR=navy]If[/COLOR] .Selected(n) [COLOR=navy]Then[/COLOR]
            ReDim Preserve Ray(c)
            Ray(c) = .List(n)
            c = c + 1
        [COLOR=navy]End[/COLOR] If
    [COLOR=navy]Next[/COLOR] n
[COLOR=navy]End[/COLOR] With
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    [COLOR=navy]For[/COLOR] n = 0 To UBound(Ray)
        [COLOR=navy]If[/COLOR] Dn = Ray(n) [COLOR=navy]Then[/COLOR]
            Dn.EntireRow.Interior.ColorIndex = 34
        [COLOR=navy]End[/COLOR] If
    [COLOR=navy]Next[/COLOR] n
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Upvote 0
Found the answer here http://www.bigresource.com/Tracker/Track-vb-IbJf1ISJUP/

Basically, the code became
Code:
Private Sub Customer_listbox_Change()

Dim i As Integer
Dim selected_item As String

i = Me.Customer_ListBox.ListIndex
selected_item = Me.Customer_ListBox.List(i)
If Me.Customer_ListBox.Selected(i) = True Then 'selected method has 0 base
  MsgBox ("You selected " & selected_item)
Else
  MsgBox ("You DESelected " & selected_item)
End If

End Sub
 
Upvote 0
Thank you Andrew

You are of course correct - there is no colouring there.

My main problem was "knowing"which item had been (de)selected. From there. it's a simple matter to select the relevant row and (de)colour it.

My problem was that I started trying to use
Code:
selected_item = Me.Customer_ListBox.[COLOR=Red]Value[/COLOR](i)
which kinda seemed logical to me. It wasn't until I found the use of .list in the reference I pointed to did I manage to get it working
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top