Selected items in a VBA Listbox lose their highlight

Becks
2StarLounger
Posts: 196
Joined: 31 Mar 2011, 03:41
Location: Perth, Western Australia

Selected items in a VBA Listbox lose their highlight

Post by Becks »

Excel VBA
Listbox with Multiselect set to fmMultiSelectMulti
aSelected = Array of booleans determined by examining active worksheet

Code: Select all

    'and update listbox
    For iCount = 0 To Me.Listbox.ListCount - 1
         Me.Listbox.Selected(iCount) = aSelected(iCount)
    Next iCount
If Wait statements were inserted into the code, the items can be seen to become highlighted but then lose the highlight soon after. Testing shows that the value of, say item 5, Listbox.Selected(5) is True at this point but is then False soon after
Complications
Listbox.ControlSource is to a range in an open but not active workbook (contain formulas dependent upon the selection state and a value in a textbox)
A frame containing a number of text boxes overlays the listbox (which will lead to another question...)

User avatar
HansV
Administrator
Posts: 79317
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Selected items in a VBA Listbox lose their highlight

Post by HansV »

Welcome to Eileen's Lounge!

I assume that you meant RowSource instead of ControlSource - there's no point in setting the ControlSource of a multiselect list box.

In a quick test, your code worked OK for me, so I suspect that there is other code that affects items being selected.
Best wishes,
Hans

Becks
2StarLounger
Posts: 196
Joined: 31 Mar 2011, 03:41
Location: Perth, Western Australia

Re: Selected items in a VBA Listbox lose their highlight

Post by Becks »

Thanks Hans
Yes, I did mean RowSource. The particular code snippet is contained in the _Change event of a 2nd control which in turn can be triggered by the user making a selection in the 2nd control, or the code forcing a change for that 2nd control (from a 3rd control)
The list box shows 12 items but contains 18 items. Clicking on the list box's vertical scroll bar will result in the list box losing its programmatic selection, but if the item is re-selected from the UI then it doesn't lose its selection when the scroll bar is used