Deselecting selected listbox item.

HighAndWilder

Active Member
Joined
Nov 4, 2006
Messages
391
Office Version
  1. 365
Platform
  1. Windows
Happy New Year Everybody

I'm not quite as happy as I would like to be but if someone can help me sort this teaser out I'll be on my way....

I select an item in a listbox and then run some code.

I set the listindex to -1 and then to deselect the apparently selected item I loop through the items and set the Selected array to false irrespective of what the current value is.

The selected item still appears to be selected after the code is run but listindex is -1.

What also happens is that the code is run twice, once when I select the item and then again when the listindex is set to -1.

If I throw a message up using MsgBox after or before I deselect all items than none of the items appear selected.

Also if I select item 0, the first one, then none of the items are selected.

Any ideas anybody? Cheers.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Is the ListBox on a UserForm or on the worksheet?

Is the ListBox set to single select mode or multi-select mode (if multi, which one)?

Can you show us the code you are using that performs as you described above?
 
Upvote 0
I think I've posted about this similar seemingly recursive problem... I'll see if I can find some links. I resolved this something like this..
Code:
Private Sub ListBox1_Click()
Dim LboxFlag As Boolean
If LboxFlag = False Then
LboxFlag = True
UserForm1.ListBox1.ListIndex = -1
Application.Wait (Now + TimeValue("0:00:01"))
UserForm11.ListBox1.Clear
UserForm11.ListBox1.AddItem "Re-Load Listbox code"
Else
LboxFlag = False
End If
End Sub
HTH. Dave
ps. should have included that where it says "Re-load Listbox Code" is where U enter your code to re-load your listbox

edit: I see Rick likely will have a better solution. Here's the link. I later added the Boolean for some reason?
https://www.mrexcel.com/forum/excel-questions/466168-phantom-listbox-selection.html
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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