Userform listbox highlight not refreshing properly

djreiswig

Well-known Member
Joined
Mar 13, 2010
Messages
523
I have a userform with a 2 listboxes. One has a list of items, the other displays items from a sheet that is filtered based on what is selected in the first listbox.

All of that works correctly, except I have a problem with the highlighting on the first listbox.

The way is it supposed to work is that when an item is selected in the first listbox, the filtering code is run which fills the second listbox and also moves the selected item to the top position in the first listbox. After the item is added to the top and then removed from it's previous position, I set the listindex to 0 so the correct item (which is now at position 0) is once again selected. I do have code that keeps the click event from firing multiple times.

The problem is that the item that is highlighted in the listbox is whatever position that was clicked. I can't get the highlight to move to the top.

I put some debug code in and it seems that the listindex is indeed set to 0, but the highlight bar isn't in the right position. Why?

I found this code

Listbox refresh problem -- mysterious phantom highlighting

but that didn't solve my problem either.

Any ideas?
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Okay, it seems like my initial description of the problem may have been too confusing. How about an example?

Add a userform to the project, and place a listbox on the userform.

Add the following code to the userform.
Code:
Dim DoThis As Boolean

Private Sub ListBox1_Click()
    'keeps this event from triggering multiple times for one click
    If DoThis = True Then
        DoThis = False
        'put the selected item at the top of the list
        ListBox1.AddItem ListBox1.Text, 0
        'remove the selected item from it's old space in the list
        ListBox1.RemoveItem ListBox1.ListIndex
        'move the selection to the top of the list (where the selected item now resides
        ListBox1.ListIndex = 0
        DoThis = True
    End If
End Sub

Private Sub UserForm_Initialize()
    'populate the listbox
    For i = 65 To 70
        ListBox1.AddItem Chr(i)
    Next i
    DoThis = True
End Sub

When you click either the first item or the last item, the list is rearranged correctly. If you select anything in the middle then it get's all messed up. The selection highlight/rectangle also doesn't behave as expected. Through the debug window it appears the listindex is indeed 0, but the listbox doesn't seem to reflect this.

Any ideas? Is there another way to go about this to get the expected results? Maybe I need to sort the list in an array, and then clear and refill the listbox. I haven't tried it that way yet, but this seems like it should work. Driving me crazy...
 
Upvote 0
I just tried the array solution, and I can get the items to go in the correct order, but the selection always moves back to the item in the same relative position as the item that was initially clicked. ie. if the third item in the list was clicked then the new third item is highlighted. I want the first item to be highlighted since that is where the old third item has been moved to. I'm thinking it has something to do with rearranging the list in the click event, but I don't know how to do it somewhere else. I tried calling a sub from the click event and putting the code there, but it seems like when the click event finishes is where the selection is moved.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,699
Messages
6,173,907
Members
452,536
Latest member
Chiz511

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