VBA Populate a list box with a range of cells without using a named range

PolarStar

New Member
Joined
Apr 25, 2016
Messages
2
Hello,

I have a vba user form with 1 combo box and 2 list boxes. Once the user makes a selection in ComboBox1 I want a corresponding set of cell values to populate the ListBox1. The user will then select one or more items from the ListBox1 which will get moved to ListBox2. Once moved to ListBox2 I want the items to be cleared (removed) from ListBox1 to help the user select more items if necessary.

For Example:
The user selects Colorado from a list of StateNames in ComboBox1, base on that selection ListBox1 is populated with all the counties in Colorado. The user then selects one or more counties from that list and moves them (with command button) to ListBox2. Once moved the selected items are removed from ListBox1 so they cannot accidentally be selected again and to make it easier to see remaining items in the list.

Initially I used a named range in the list box row source property, but have learned that you cannot clear selected items from a named range. What is another way to populate a list box with cell values in a specific column?

My data looks like this, but would have all 50 states and all counties listed:
ABC
1ColoradoIllinoisArizona
2San JuanDuPage
Navajo
3QurayDeKalbApache
4GunnisonKaneCochise
5PitkinCoconino
6CusterYuma
7Pueblo
8Las Animas
9

<tbody>
</tbody>
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this:
This assumes all you State name are in row(1) column 1 to 50
And the counties are in the row(2) and below for each county in that state.

And your Listbox1 must be set for Multiselect in the properties window

And you have 1 combobox and Two Listboxes

And one command Button

You will see how I named my controls.
I use default names.

When you select a State from Combobox1
All the counties for that State will be loaded into Listbox1
When you select the counties in Listbox two you want added to Listbox2
You then need to click Command button1

The values selected in Listbox1 will be copied to Listbox2 and removed from Listbox1

I believe this is what you wanted.

You need to put these three scripts in your Userform

Code:
Private Sub ComboBox1_Change()
'Modified  10/12/2018  1:15:36 AM  EDT
ListBox2.Clear
Dim c As Long
Dim Lastrow As Long
Set SearchRange = Cells(1, 1).Resize(, 50).Find(ComboBox1.Value)
c = SearchRange.Column
Lastrow = Cells(Rows.Count, c).End(xlUp).Row
ListBox1.Clear
    For i = 2 To Lastrow
        ListBox1.AddItem Cells(i, c).Value
    Next
End Sub
Private Sub CommandButton2_Click()
'Modified  10/12/2018  1:15:36 AM  EDT
Dim i As Long
Dim b As Long
For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) = True Then
        ListBox2.AddItem ListBox1.List(i)
    End If
Next i
For b = ListBox1.ListCount - 1 To 0 Step -1
    If ListBox1.Selected(b) = True Then
        ListBox1.RemoveItem (b)
    End If
Next b
End Sub
Private Sub UserForm_Initialize()
'Modified  10/12/2018  1:15:36 AM  EDT
Dim i As Long
    For i = 1 To 50
        ComboBox1.AddItem Cells(1, i).Value
    Next
End Sub
 
Upvote 0
Thanks for your response. It worked great although I did have to Dim i and SearchRange. I am new to VBA but was able to figure it out. Thanks again. I appreciate or taking time to help.
 
Upvote 0
Thanks for your response. It worked great although I did have to Dim i and SearchRange. I am new to VBA but was able to figure it out. Thanks again. I appreciate or taking time to help.

Yes I forget some times to Dim all things.
Strange it works for me without Dimming but not others.
I need to start remembering that more.

Glad it worked for you.
I was wondering if you would come back and say if it worked.
It's always nice to hear from Posters if things worked for them.
Take care

I even did a search on the net and found listings of all counties in all states and adding them to a few state columns just as a test.
 
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,089
Members
448,548
Latest member
harryls

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