I'm using Excel2003 VBA, and I have a single-select listbox which, for example, I populate with 4 items:
listbox1.additem "ITEM 1"
listbox1.additem "ITEM 2"
listbox1.additem "ITEM 3"
listbox1.additem "ITEM 4"
The listbox1_click event subroutine shows:
if listbox1 = "ITEM 3" then
DISPLAY2
endif
The DISPLAY2 routine has
listbox1.clear
listbox1.listindex = -1 (might be redundant, I think, since I cleared it?)
listbox1.additem "NEW ITEM 1"
listbox1.additem "NEW ITEM 2"
listbox1.additem "NEW ITEM 3"
listbox1.additem "NEW ITEM 4"
For some reason, when I select ITEM 3 in the first display, and the second display comes up with the new 4 items, it has NEW ITEM 3 highlighted. I've tried putting the listbox1.listindex = -1 or listbox1.listindex = null below the additem entries, and I've tried changing the listbox1.visible = false before populating it, and listbox1.visible = true after populating it (thinking that a repaint might fix this), but I cannot get it to display the NEW ITEM screen with nothing highlighted. Can you see what I'm doing wrong, or a workaround to this? My best workaround I could think of was to create a listbox2 for the NEW ITEM list, and make the original listbox invisible and the new one visible after I switch, but I don't see why I can't re-use the same listbox, other than this highlighting problem. (It seems to me that it may be detecting a mouse event when it goes to DISPLAY2, and thereby highlighting the area where my mouse is still hovering, but if that's it, I can't think of a way to bypass this situation.) I've also utilized a text box to display the current LISTINDEX value, and strangely, when the macro displays the NEW ITEM list, and it highlights the 3rd item, the listindex displays properly as "-1", although based on what it's highlighting, it should be showing "2".
Thanks!
Full Example:
Private Sub UserForm_Activate()
Display1
End Sub
Private Sub Display1()
ListBox1.Clear
ListBox1.AddItem "ITEM 1"
ListBox1.AddItem "ITEM 2"
ListBox1.AddItem "ITEM 3"
ListBox1.AddItem "ITEM 4"
End Sub
Private Sub ListBox1_Click()
Label1 = ListBox1
If ListBox1 = "ITEM 3" Then
Display2
End If
End Sub
Private Sub Display2()
ListBox1.Clear
ListBox1.AddItem "NEW ITEM 1"
ListBox1.AddItem "NEW ITEM 2"
ListBox1.AddItem "NEW ITEM 3" (this item is highlighted automatically when I selected ITEM 3)
ListBox1.AddItem "NEW ITEM 4"
End Sub
Bookmarks