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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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,214,641
Messages
6,120,685
Members
448,977
Latest member
dbonilla0331

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