Search code examples
vbaexcellistboxuserform

Excel-VBA UserForm ListBox - How can I Highlight option without Selecting


Nutshell:

What is the equivalent to Me.ListBox1.Selected(2) = True but for highlighting instead of Selecting?? (Highlight might be the wrong word but I'm sure I've seen that before in this context - the outline box around an option instead of actually selecting it)

** Tried **

Me.ListBox1.ListIndex = 2

doesn't do anything either.

With Me.ListBox1
    saveVal = .List(0)
    .Value = ""
    .Value = saveVal
End With

Gives me an error saying "Invalid Property Value"

Background:

Why do I need to click twice on some items/boxes?

I have a userform with several ListBox which are populated by adding items one by one, which works fine.

The ListBox settings have been changed to MultiSelect = 1 - fmMultiSelectMulti and ListStyle = 1 - fmListStyleOption

The issue is when trying to select an option on some boxes for the first time, it just highlights it without selecting it, while on other boxes and other tries it may or may not select an option straight away. It doesn't seem to be the same culprits and is a bit random - does it have something to do with where I'm clicking on the option? On the item text instead of the item box? Why does it work on some and not others?

After I've selected one option in a box, it then allows me to select any other with a single click which is the desired behaviour.

I found a link where someone had suggested this behaviour was because of a corrupted workbook, but I don't think it's that. I just created a new book with 1 listbox in a userform, changed to desired settings, copied the listbox 6 times, put the listsource as rand() in columns A:G and I'm still getting the same issue.

Edit: My plan is to "highlight" the top option on each box so that the first click on a box has no option other than to select (because something is already highlighted)...


Solution

  • The trick to tackling my INITIAL problem seems to have been after populating each list to set .ListIndex = 0 but this only worked after using .SetFocus

    Solved! (For now, I think)

    Even though this doesn't highlight anything it allows me to select something first time without fail.