Find default value in a ListBox

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,341
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?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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
 
Upvote 0
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.
 
Upvote 0
You are welcome and thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,816
Members
449,049
Latest member
cybersurfer5000

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