Find default value in a ListBox

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,231
I have two ListBoxes on a userform. Selecting a value in ListBox1 puts a value into a cell. This in turn creates a value in a cell read by ListBox 2. The code then re-initializes ListBox 2 and the matching item is highlighted in ListBox2.
When I try to identify the highlighted value in ListBox 2 my code returns "". If I click the highlighted value first, the code then returns the highlighted value.
My understanding is that the ListBox2.value returns only the selected value, but not the default value. It seems silly that to make the code work, I have to click an already highlighted item. Can anyone suggest a solution?
 

Some videos you may like

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
My understanding is that the ListBox2.value returns only the selected value, but not the default value.
The "default" value of a ListBox is empty ("") until user makes a selection. In cases of a MultiSelect ListBox the Value property will be empty also.

The code then re-initializes ListBox 2 ......
.... meaning clearing all items?

.... and the matching item is highlighted in ListBox2.
... how does your code determine to highlight the right item?
When there is just one item in your ListBox you might use this
VBA Code:
    ListBox2.Selected(0) = True
 

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,231
Thanks for your reply GWteB and for confirmation that the highlighted item is not "selected". It looks suspiciously like a bug to me. I think the workaround must be to ensure that there is no blank item that can be selected and then to check if the "selection' is empty and if so to take instead the value that the ListBox read to take the highlight.
Your comments are much appreciated.
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
You are welcome and thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,814
Messages
5,574,487
Members
412,597
Latest member
Timtec
Top