How to fill a multiselect listbox from a cell

santnok

Board Regular
Joined
Jan 10, 2014
Messages
97
Hi

I have found this code on the net

Code:
Dim arrItems()
Dim cnt As Long
Dim I As Long


    For I = 0 To ListGateWay.ListCount - 1
        If ListGateWay.Selected(I) Then
            ReDim Preserve arrItems(cnt)
            arrItems(cnt) = ListGateWay.List(I)
            cnt = cnt + 1
        End If
    Next I


    If cnt > 0 Then
        ws.Cells(LastRow + 1, "U").Value = Join(arrItems, ",")
    End If

This code work perfect when I use it, I have an save button that I use the code and in one cell it save what I have selected in the listbox.
My problem are, when I open my userform and the product that I have saved I like also the listbox to fill out what have been saved in the cell.

Ex: if the listbox have

Product 1
Product 2
Product 3

I have selcted Produkt 1 and 2 and I have saved.
Now in my sheet and the cell it will be: Product 1,Product 2

I now open my userform and I will now fill the listbox with the information that are saved in the cell.

Hope someone understand what I mean here :-)
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try something like this. Change the sheet name to suit.

Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] UserForm_Initialize()
    
    [color=darkblue]Dim[/color] v [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    [color=darkblue]Dim[/color] I [color=darkblue]As[/color] [color=darkblue]Long[/color]
    
    v = Sheets("[B]Sheet1[/B]").Cells(Rows.Count, "U").End(xlUp).Value
    v = Split(v, ",")
    
    [color=darkblue]With[/color] ListGateWay
        [color=darkblue]For[/color] I = 0 [color=darkblue]To[/color] .ListCount - 1
            .Selected(I) = IsNumeric(Application.Match(.List(I), v, 0))
        [color=darkblue]Next[/color] I
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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