Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Userform Listbox highlights wrong line.

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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cursor highlights the wrong rows in excel? Splashdown Excel Discussion (Misc queries) 0 March 30th 10 03:24 PM
userform listbox cannot get listbox.value to transfer back to main sub [email protected] Excel Programming 1 May 17th 06 09:44 PM
listbox rowsource from wrong workbook David Sisson[_2_] Excel Programming 2 October 28th 05 02:13 PM
Wrong type of Listbox from VBA Michael Singmin Excel Programming 5 June 26th 04 10:05 PM
Populating multicolumn listbox the wrong way notsureofthatinfo Excel Programming 0 October 22nd 03 11:38 PM


All times are GMT +1. The time now is 09:00 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"