Listbox question

dave8

Active Member
Joined
Jul 8, 2007
Messages
275
I have a listbox on a worksheet. The properties on this listbox is [1-fmMultiSelectMulti] and [1-fmListStyleOption]. So just to clarify, the items on the listbox is such that there are checkboxes besides the list of items. In the Listbox1_Change procedure, I have code to loop thru Listbox1.selected(i). So, if an item is selected, I have code to do something. But, I also want to do something if an item is de-selected, so to speak. Is there a way to do this??
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
On cursory testing this seemed to work:
Code:
Private Sub ListBox1_Change()
Static LastSel()
Dim SelNow()
ReDim SelNow(0 To Me.ListBox1.ListCount - 1)
Set ccc = Me.ListBox1
For i = 0 To Me.ListBox1.ListCount - 1
  SelNow(i) = Me.ListBox1.Selected(i)
  If SelNow(i) <> LastSel(i) Then
    MsgBox Me.ListBox1.List(i) & " has been " & IIf(SelNow(i), "", "de-") & "selected"
  End If
Next i
LastSel() = SelNow()
End Sub
 
Upvote 0
addendum:
Discovered a problem when LastSel array is not initialized. Here I initialize it on the worksheet being activated - it doesn't have to be there, it could be on some other event:
Code:
Dim EstablishLastSel As Boolean

Private Sub ListBox1_Change()
Static LastSel()
Dim SelNow()
If EstablishLastSel Then
ReDim LastSel(0 To Me.ListBox1.ListCount - 1)
  For i = 0 To Me.ListBox1.ListCount - 1
    LastSel(i) = Me.ListBox1.Selected(i)
  Next i
  EstablishLastSel = False
Else
  ReDim SelNow(0 To Me.ListBox1.ListCount - 1)
  Set ccc = Me.ListBox1
  For i = 0 To Me.ListBox1.ListCount - 1
    SelNow(i) = Me.ListBox1.Selected(i)
    If SelNow(i) <> LastSel(i) Then
      MsgBox Me.ListBox1.List(i) & " has been " & IIf(SelNow(i), "", "de-") & "selected"
    End If
  Next i
  LastSel() = SelNow()
End If
End Sub

Private Sub Worksheet_Activate()
EstablishLastSel = True
ListBox1_Change
End Sub
ps. LastSel = Last Selection
SelNow = Selected Now
 
Upvote 0
First, thanks for taking a moment to think of solution. I can't get this to work. I'm getting a subscript out of range error at ' If SelNow(i) <> LastSel(i) Then ' line.
 
Upvote 0
First, thanks for taking a moment to think of solution. I can't get this to work. I'm getting a subscript out of range error at ' If SelNow(i) <> LastSel(i) Then ' line.
In the error box choose End.
Then select another sheet, reselect the listbox sheet and try again. It's the business of establishing LastSel() that I mentioned.

Also make sure that the line:
Dim EstablishLastSel As Boolean
is above all other subs/functions in the module.
 
Last edited:
Upvote 0
I got pass the error but it's never displaying the Msgbox. I select/de-select from the listbox.
 
Upvote 0
Well I tested it here and it works.
It's likley that you may have some different arrangement from mine (1, single column Listbox on a sheet and all the code in that sheet's code module. If you want, post a copy of the workbook on the internet, say at box.net, or send me a copy privately (Private Message me here at mrexcel with a request for my email address) and I will try to establish what's going wrong.
 
Upvote 0
Firstly, the property on my listbox is not set as single. It's set as fmMultiSelectMulti and fmListStyleOption. My workbook is about 2mb, but the section of code is handled the same you provided me. Another note is that my listbox is on a worksheet and not on a userform.
 
Upvote 0
Firstly, the property on my listbox is not set as single. It's set as fmMultiSelectMulti and fmListStyleOption.
Yep, so's mine. I said it was single column.
My workbook is about 2mb, but the section of code is handled the same you provided me.
Fine.
Another note is that my listbox is on a worksheet and not on a userform.
Mine's on a worksheet too.

If it bugs out press End, switch away, then back, to the sheet.
To be certain the code's not in break mode after exploring with Debug, click the reset button (or Run|Reset in the VBE). Switch away and back to the sheet and try again.
Of course, ensure that macros are enabled.
 
Upvote 0
I created a separate workbook with your code. Can I send it to you so you can look at what I'm doing incorrectly? It's still not displaying the msgbox. Although if I run it in debug mode, it works fine. That is, execute each statement line by line.
 
Upvote 0

Forum statistics

Threads
1,214,383
Messages
6,119,198
Members
448,874
Latest member
Lancelots

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