Sorting Multiple Listboxes With Lists That Are Dependent Upon Each Other

CaliKidd

Board Regular
Joined
Feb 16, 2011
Messages
173
Imagine a userform with 3 listboxes (lbAnswer1, lbAnswer2, lbAnswer3). When the form is initialized, all of the listboxes are populated with the letters of the alphabet, like this:

Code:
With lbAnswer1
[INDENT].addItem "A"
.addItem "B"
.addItem "C"
...etc
.addItem "Z"
[/INDENT]End With
With lbAnswer2
[INDENT].addItem "A"
.addItem "B"
.addItem "C"
...etc
.addItem "Z"
[/INDENT]End With
With lbAnswer3
[INDENT].addItem "A"
.addItem "B"
.addItem "C"
...etc
.addItem "Z"
[/INDENT]End With

OK, now let me describe my objective conceptually.

1. When the form is run, I want the user to be able to click the dropdown on each listbox and choose a letter. I want that selected letter to be removed from the other listboxes so it cannot be chosen again (i.e., no duplicates). For example, if the user selects the letter "M" on the second listbox, the letter "M" should be removed from the list of selectable entries from the first and third listboxes.

2. However, if the user changes his mind and decides to choose the letter "K" instead of "M", the letter "K" needs to be removed from the other lists and the previously selected letter "M" needs to be added back into the lists.

3. Additionally, the lists need to be alphabetically sorted so the newly added letter "M" does not appear at the end of the list, but rather where it should belong alphabetically.

4. Finally, the user should be able to select the listboxes in any order, not be required to select them sequentially. In other words, they should not have to select the first listbox, then the second, then the third, although they should be free to go in this or any other order they choose.

I hope that is clear. :eek:

OK, so I'm wondering what the VBA code would look like to accomplish this. I've been struggling with this all day and haven't found a solution in any of the forums.

Any ideas?
 
Last edited:
The .Tag property is just a place to hold string data. All userforms and their controls each have a .Tag property.
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi, I see from Mike's code what you where trying to achieve !!

Mike, I think you bit your bit of code and the logic behind it is very clever.
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,217,367
Messages
6,136,133
Members
449,994
Latest member
Rocky Mountain High

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