Phantom Listbox selection

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
3,724
I've made a trial to demonstrate. Add data to A1:B10 and a Userform with a Listbox. The following code is supposed to load the listbox with A1:A10 (and "TEST) on initialize and then when "TEST" is selected the Listbox clears and loads B1:10. Simple enough, but how to clear the phantom selection in the listbox (it's coded to = -1)? When "TEST" is selected, the Listbox updates but keeps the same listitem selected in the new list (unless the first listitem is selected and then it works as expected?). A sort of remedy is to use Application.Wait. It's probably something simple...maybe a listbox setting thing? Anyways, any help is always appreciated. Dave
Code:
Private Sub UserForm_Initialize()
UserForm1.ListBox1.ListStyle = fmListStyleOption
UserForm1.ListBox1.MultiSelect = fmMultiSelectSingle
For Cnt1 = 1 To 10
UserForm1.ListBox1.AddItem "TEST"
UserForm1.ListBox1.AddItem Sheets("Sheet1").Range("A" & Cnt1).Value
Next Cnt1
End Sub

Private Sub ListBox1_Click()
If UserForm1.ListBox1.Value = "TEST" Then
UserForm1.ListBox1.ListIndex = -1
'Application.Wait (Now + TimeValue("0:00:01"))
UserForm1.ListBox1.Clear
For Cnt2 = 1 To 10
UserForm1.ListBox1.AddItem Sheets("Sheet1").Range("B" & Cnt2).Value
Next Cnt2
End If
End Sub

ps. XL03 code
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
bump....Anyone know how to get rid of the listbox selection after clearing a listbox and then reloading it? Dave
 
Upvote 0
Try:
Code:
Private Sub UserForm_Initialize()
    With Me.ListBox1
        .ListStyle = fmListStyleOption
        .MultiSelect = fmMultiSelectSingle
        .AddItem "TEST"
        For Cnt1 = 1 To 10
            .AddItem Sheets("Sheet1").Range("A" & Cnt1).Value
        Next Cnt1
    End With
End Sub

Private Sub ListBox1_Click()
    With Me.ListBox1
        If .Value = "TEST" Then
            .Clear
            'Application.Wait (Now + TimeValue("0:00:01"))
            For Cnt2 = 1 To 10
                .AddItem Sheets("Sheet1").Range("B" & Cnt2).Value
            Next Cnt2
            .ListIndex = -1
        End If
    End With
End Sub
 
Upvote 0
Thank you very much for your time Rory. Unfortunately, that code produces the same end result. It does work if you click "TEST" when it is the first list item, but if "TEST" is any other list item, then the new list has the same list item selected (rather than no list item selected). My initialization code has "TEST" as every other list item for testing purposes. Again, thank you for your time. Do you have any other suggestions? Dave
 
Upvote 0
As far as I can tell it is, as you said, simply a timing issue. If you step through the code, it works fine as it does if you add a long enough Wait.
There is no setting that affects this, so I'd suggest using two listboxes instead (if that's applicable) or using a button rather than the click event.
 
Upvote 0
I finally resolved this issue so I thought I'd post the solution. Perhaps it will be helpful. Dave
ps. This code uses userform1 with listbox1 and data in Sheet1 A1:B10. On initialize listbox1 loads A1:A10. On listbox click(or doubleclick) it empties and loads B1:B10 ....
and the phantom listbox selection is gone....yeah!!!
Code:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
UserForm1.ListBox1.Enabled = False
Application.Wait (Now + TimeValue("0:00:02"))
UserForm1.ListBox1.Enabled = True
End Sub

Private Sub UserForm_Initialize()
UserForm1.ListBox1.ListStyle = fmListStyleOption
UserForm1.ListBox1.MultiSelect = fmMultiSelectSingle
For Cnt1 = 1 To 10
UserForm1.ListBox1.AddItem Sheets("Sheet1").Range("A" & Cnt1).Value
Next Cnt1
End Sub

Private Sub ListBox1_Click()
UserForm1.ListBox1.ListIndex = -1
Application.Wait (Now + TimeValue("0:00:01"))
UserForm1.ListBox1.Clear
For Cnt2 = 1 To 10
UserForm1.ListBox1.AddItem Sheets("Sheet1").Range("B" & Cnt2).Value
Next Cnt2
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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