Userform Listbox highlights wrong line.

A

Alan Carpenter

Userform Listbox highlights wrong line.

I am using a Listbox (List1) on a Userform (frmReports) to present eight
columns and on average from one to about 15 (sometimes, but rarely, up to
100) rows of information from four sheets in a Workbook, each sheet with
~100,000 rows. The user double clicks a cell of interest, my VBA goes a-
hunting and the Listbox presents the lucky user with exactly the right
info, every time.
One of the Listbox lines will be relevant to the user, and I want this line
to be both the Active line and to be Highlighted. I save the correct number
for the zero based index into my variable mLine and tell the Listbox to do
its thing:

List1.ListIndex = mLine
List1.Selected(mLine) = True
End Sub

That's the last line of some quite complex calculations starting from the
Enter Event of the Listbox.

Oh, and ListBox1.MultiSelect = fmMultiSelectSingle

All is wonderful. Until!
Every once in a while the highlighted row is incorrect. mLine is correct,
the Listbox is correct, but the highlight will be on the wrong line. I
found no difference with this:
List1.ListIndex = mLine
List1.Selected(mLine) = True
Me.Repaint
End Sub

The Zero base is not an issue.
As a debugging aid I added this.
....
ListBox1.ListIndex = -1 'Reset
List1.ListIndex = mLine 'mLine is correct
List1.Selected(mLine) = True
Me.Repaint
If mLine <> Me.List1.ListIndex Then MsgBox Me.List1.ListIndex
frmReports.Caption = mCaption & " " & List1.RowSource _
& " Index=" & List1.ListIndex _
& " mLine= " & mLine _
List1.Selected(mLine)
End Sub

Still the eror happens. For example, in a Listbox with 20 lines I can see
mLine =3, ListIndex = 3, True, but the highlight will be on (e.g.) line 12.

A clue - DoEvents here ...

DoEvents
List1.ListIndex = mLine
List1.Selected(mLine) = True
DoEvents
Me Repaint

.... seems to make the error happen less often.

The RowSource and nLine are set before frmReports.Show, and the code is in
the List1 Enter Event. It's the only code in the Form, it *is* being
executed and everything is exactly as I want except for the wrong line
being highlighted as selected.

So, in a Listbox with 10 rows, mLine = 3 (verified before and after),
running this code and no following code:

DoEvents
List1.ListIndex = mLine
List1.Selected(mLine) = True
DoEvents
Me Repaint

There's a huge amount of code before this stage, but this is the final bit.
I can as a test output the info to a Worksheet, to a Text file, to
MSAccess or to MSWord and the information itself is always correct.

What could cause highlighting of the wrong line, while ListIndex is
correct?
What don't I know that I don't know that I don't know?

How can it be, when List1.Selected(3) = True, with DoEvents before and
after and a Repaint after, that the highlight is not the fourth Line?

I'd love a "D'oh!" moment right now.

Cheers,
Alan Carpenter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top