Retain selection in ActiveX list box

bv182

New Member
Joined
Jul 6, 2011
Messages
8
Hi,

I am using ActiveX list box with multiple selection. Now Whenever I close the workbook and then reopen, all the previous selections are gone.

Is there a way to retain selections made in listbox?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
One way might be to use a hidden sheet to store the indices for the selected items in the list box when saving the workbook, and then use those indices to restore the selections when opening the workbook. So, for example, let's say that Sheet1 contains the ActiveX list box named 'ListBox1', and that the indices are to be stored in Sheet3, which is hidden. First, place the following code in the 'ThisWorkbook' code module (double-click the icon for ThisWorkbook located in the Project Explorer Window)...

Code:
[font=Verdana][color=darkblue]Private[/color] [color=darkblue]Sub[/color] Workbook_BeforeSave([color=darkblue]ByVal[/color] SaveAsUI [color=darkblue]As[/color] [color=darkblue]Boolean[/color], Cancel As [color=darkblue]Boolean[/color])
    [color=darkblue]Call[/color] StoreSelections
[color=darkblue]End[/color] [color=darkblue]Sub[/color]

[color=darkblue]Private[/color] [color=darkblue]Sub[/color] Workbook_Open()
    [color=darkblue]Call[/color] RestoreSelections
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]

Then place the following code in a regular module (Insert > Module)...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] RestoreSelections()

    [color=darkblue]Dim[/color] SourceRng [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] Cell [color=darkblue]As[/color] Range
    
    [color=darkblue]With[/color] Worksheets("Sheet3")
        [color=darkblue]Set[/color] SourceRng = .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] Cell [color=darkblue]In[/color] SourceRng
        [color=darkblue]If[/color] Cell <> "" [color=darkblue]Then[/color]
            Worksheets("Sheet1").ListBox1.Selected(Cell.Value) = [color=darkblue]True[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] Cell

[color=darkblue]End[/color] [color=darkblue]Sub[/color]

[color=darkblue]Sub[/color] StoreSelections()

    [color=darkblue]Dim[/color] MyArray() [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] Cnt [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]

    Worksheets("Sheet3").Columns("A").Clear
        
    [color=darkblue]With[/color] Worksheets("Sheet1").ListBox1
        [color=darkblue]For[/color] i = 0 [color=darkblue]To[/color] .ListCount - 1
            [color=darkblue]If[/color] .Selected(i) [color=darkblue]Then[/color]
                Cnt = Cnt + 1
                [color=darkblue]ReDim[/color] [color=darkblue]Preserve[/color] MyArray(1 [color=darkblue]To[/color] Cnt)
                MyArray(Cnt) = i
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]Next[/color] i
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    [color=darkblue]If[/color] Cnt > 0 [color=darkblue]Then[/color]
        Worksheets("Sheet3").Range("A1").Resize(UBound(MyArray)) = WorksheetFunction.Transpose(MyArray)
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
End Sub
[/font]
 
Upvote 0
Thanks Domenic.

The problem is I have around 50 list-box in my excel file. Is there an easier way of doing this. Otherwise I will go for this code.

Thanks

One way might be to use a hidden sheet to store the indices for the selected items in the list box when saving the workbook, and then use those indices to restore the selections when opening the workbook. So, for example, let's say that Sheet1 contains the ActiveX list box named 'ListBox1', and that the indices are to be stored in Sheet3, which is hidden. First, place the following code in the 'ThisWorkbook' code module (double-click the icon for ThisWorkbook located in the Project Explorer Window)...

Code:
[FONT=Verdana][COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Workbook_BeforeSave([COLOR=darkblue]ByVal[/COLOR] SaveAsUI [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Boolean[/COLOR], Cancel As [COLOR=darkblue]Boolean[/COLOR])
    [COLOR=darkblue]Call[/COLOR] StoreSelections
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Workbook_Open()
    [COLOR=darkblue]Call[/COLOR] RestoreSelections
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
[/FONT]
Then place the following code in a regular module (Insert > Module)...

Code:
[FONT=Verdana][COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] RestoreSelections()

    [COLOR=darkblue]Dim[/COLOR] SourceRng [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] Cell [COLOR=darkblue]As[/COLOR] Range
    
    [COLOR=darkblue]With[/COLOR] Worksheets("Sheet3")
        [COLOR=darkblue]Set[/COLOR] SourceRng = .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] Cell [COLOR=darkblue]In[/COLOR] SourceRng
        [COLOR=darkblue]If[/COLOR] Cell <> "" [COLOR=darkblue]Then[/COLOR]
            Worksheets("Sheet1").ListBox1.Selected(Cell.Value) = [COLOR=darkblue]True[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]Next[/COLOR] Cell

[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

[COLOR=darkblue]Sub[/COLOR] StoreSelections()

    [COLOR=darkblue]Dim[/COLOR] MyArray() [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] Cnt [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]

    Worksheets("Sheet3").Columns("A").Clear
        
    [COLOR=darkblue]With[/COLOR] Worksheets("Sheet1").ListBox1
        [COLOR=darkblue]For[/COLOR] i = 0 [COLOR=darkblue]To[/COLOR] .ListCount - 1
            [COLOR=darkblue]If[/COLOR] .Selected(i) [COLOR=darkblue]Then[/COLOR]
                Cnt = Cnt + 1
                [COLOR=darkblue]ReDim[/COLOR] [COLOR=darkblue]Preserve[/COLOR] MyArray(1 [COLOR=darkblue]To[/COLOR] Cnt)
                MyArray(Cnt) = i
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]Next[/COLOR] i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    [COLOR=darkblue]If[/COLOR] Cnt > 0 [COLOR=darkblue]Then[/COLOR]
        Worksheets("Sheet3").Range("A1").Resize(UBound(MyArray)) = WorksheetFunction.Transpose(MyArray)
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
End Sub
[/FONT]
 
Upvote 0
Place code into ThisWorkbook module.
Code:
[COLOR="Blue"]Private[/COLOR] [COLOR="Blue"]Sub[/COLOR] Workbook_BeforeSave([COLOR="Blue"]ByVal[/COLOR] SaveAsUI [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Boolean[/COLOR], Cancel [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Boolean[/COLOR])
    [COLOR="Blue"]Call[/COLOR] SaveSelections
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]

[COLOR="Blue"]Private[/COLOR] [COLOR="Blue"]Sub[/COLOR] Workbook_Open()
    [COLOR="Blue"]Call[/COLOR] RestoreSelections
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]

[COLOR="Blue"]Private[/COLOR] [COLOR="Blue"]Sub[/COLOR] SaveSelections()
    
    [COLOR="Blue"]Dim[/COLOR] arr() [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Variant[/COLOR], i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR], j [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    
    [COLOR="Blue"]With[/COLOR] Sheet1.OLEObjects("ListBox1").Object
        [COLOR="Blue"]If[/COLOR] .ListIndex > 0 [COLOR="Blue"]Then[/COLOR]
            [COLOR="Blue"]For[/COLOR] i = 0 [COLOR="Blue"]To[/COLOR] .ListCount - 1
                [COLOR="Blue"]If[/COLOR] .Selected(i) [COLOR="Blue"]Then[/COLOR]
                    j = j + 1
                    [COLOR="Blue"]ReDim[/COLOR] [COLOR="Blue"]Preserve[/COLOR] arr(1 [COLOR="Blue"]To[/COLOR] j)
                    arr(j) = i
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]Next[/COLOR]
            Names.Add Name:="Selections", RefersTo:=arr, Visible:=[COLOR="Blue"]False[/COLOR]
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
    
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]

[COLOR="Blue"]Private[/COLOR] [COLOR="Blue"]Sub[/COLOR] RestoreSelections()
    
    [COLOR="Blue"]Dim[/COLOR] arr [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Variant[/COLOR], i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR]

    arr = [Selections]
        
    [COLOR="Blue"]With[/COLOR] Sheet1.OLEObjects("ListBox1").Object
        [COLOR="Blue"]For[/COLOR] i = 1 [COLOR="Blue"]To[/COLOR] [COLOR="Blue"]UBound[/COLOR](arr)
            .Selected(arr(i)) = [COLOR="Blue"]True[/COLOR]
        [COLOR="Blue"]Next[/COLOR]
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
    
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Thanks Domenic.

You're welcome!

The problem is I have around 50 list-box in my excel file. Is there an easier way of doing this. Otherwise I will go for this code.

Based on the same assumptions, where Sheet1 contains the ListBoxes, and Sheet3 is hidden and used to store the indices for the ListBox selections, try the following...

1) The code for 'ThisWorkbook' remains the same.

2) The code for the regular module changes as follows...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] RestoreSelections()

    [color=darkblue]Dim[/color] LastRow [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] LastColumn [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] j [color=darkblue]As[/color] [color=darkblue]Long[/color]

    [color=darkblue]With[/color] Worksheets("Sheet3")
        LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] LastColumn
            [color=darkblue]If[/color] .Cells(1, i).Value <> "" [color=darkblue]Then[/color]
                LastRow = .Cells(.Rows.Count, i).End(xlUp).Row
                [color=darkblue]For[/color] j = 2 [color=darkblue]To[/color] LastRow
                    Worksheets("Sheet1").OLEObjects(.Cells(1, i).Value).Object.Selected(.Cells(j, i)) = [color=darkblue]True[/color]
                [color=darkblue]Next[/color] j
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]Next[/color] i
    [color=darkblue]End[/color] [color=darkblue]With[/color]

[color=darkblue]End[/color] [color=darkblue]Sub[/color]

[color=darkblue]Sub[/color] StoreSelections()

    [color=darkblue]Dim[/color] OleObj [color=darkblue]As[/color] OLEObject
    [color=darkblue]Dim[/color] MyArray() [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] NextColumn [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] Cnt [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
    
    Worksheets("Sheet3").Cells.Clear

    NextColumn = 1
    [color=darkblue]For[/color] [color=darkblue]Each[/color] OleObj [color=darkblue]In[/color] Worksheets("Sheet1").OLEObjects
        [color=darkblue]If[/color] TypeName(OleObj.Object) = "ListBox" [color=darkblue]Then[/color]
            [color=darkblue]With[/color] OleObj.Object
                [color=darkblue]For[/color] i = 0 [color=darkblue]To[/color] .ListCount - 1
                    [color=darkblue]If[/color] .Selected(i) [color=darkblue]Then[/color]
                        Cnt = Cnt + 1
                        [color=darkblue]ReDim[/color] [color=darkblue]Preserve[/color] MyArray(1 [color=darkblue]To[/color] Cnt)
                        MyArray(Cnt) = i
                    [color=darkblue]End[/color] [color=darkblue]If[/color]
                [color=darkblue]Next[/color] i
            [color=darkblue]End[/color] [color=darkblue]With[/color]
            [color=darkblue]If[/color] Cnt > 0 [color=darkblue]Then[/color]
                [color=darkblue]With[/color] Worksheets("Sheet3")
                    .Cells(1, NextColumn) = OleObj.Name
                    .Cells(2, NextColumn).Resize(UBound(MyArray)).Value = WorksheetFunction.Transpose(MyArray)
                [color=darkblue]End[/color] [color=darkblue]With[/color]
                NextColumn = [color=darkblue]Next[/color]Column + 1
                Erase MyArray
                Cnt = 0
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    Next OleObj
    
[color=darkblue]End[/color] Sub
[/font]
 
Upvote 0
I tried this but it didn't work for me!

Actually I have 5 ActiveX list boxes in sheet1 and the indices are all in sheet3.
I put the codes which you gave in “This Workbook” and “Module 1” but the problem is that when I select the check boxes and save the file all the ActiveX list boxes and indices become cleared and disappeared.
Please help me with that.



You're welcome!


Based on the same assumptions, where Sheet1 contains the ListBoxes, and Sheet3 is hidden and used to store the indices for the ListBox selections, try the following...

1) The code for 'ThisWorkbook' remains the same.

2) The code for the regular module changes as follows...

Code:
[FONT=Verdana][COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] RestoreSelections()

    [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] LastColumn [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] j [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]

    [COLOR=darkblue]With[/COLOR] Worksheets("Sheet3")
        LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] LastColumn
            [COLOR=darkblue]If[/COLOR] .Cells(1, i).Value <> "" [COLOR=darkblue]Then[/COLOR]
                LastRow = .Cells(.Rows.Count, i).End(xlUp).Row
                [COLOR=darkblue]For[/COLOR] j = 2 [COLOR=darkblue]To[/COLOR] LastRow
                    Worksheets("Sheet1").OLEObjects(.Cells(1, i).Value).Object.Selected(.Cells(j, i)) = [COLOR=darkblue]True[/COLOR]
                [COLOR=darkblue]Next[/COLOR] j
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]Next[/COLOR] i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]

[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

[COLOR=darkblue]Sub[/COLOR] StoreSelections()

    [COLOR=darkblue]Dim[/COLOR] OleObj [COLOR=darkblue]As[/COLOR] OLEObject
    [COLOR=darkblue]Dim[/COLOR] MyArray() [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] NextColumn [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] Cnt [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    Worksheets("Sheet3").Cells.Clear

    NextColumn = 1
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] OleObj [COLOR=darkblue]In[/COLOR] Worksheets("Sheet1").OLEObjects
        [COLOR=darkblue]If[/COLOR] TypeName(OleObj.Object) = "ListBox" [COLOR=darkblue]Then[/COLOR]
            [COLOR=darkblue]With[/COLOR] OleObj.Object
                [COLOR=darkblue]For[/COLOR] i = 0 [COLOR=darkblue]To[/COLOR] .ListCount - 1
                    [COLOR=darkblue]If[/COLOR] .Selected(i) [COLOR=darkblue]Then[/COLOR]
                        Cnt = Cnt + 1
                        [COLOR=darkblue]ReDim[/COLOR] [COLOR=darkblue]Preserve[/COLOR] MyArray(1 [COLOR=darkblue]To[/COLOR] Cnt)
                        MyArray(Cnt) = i
                    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
                [COLOR=darkblue]Next[/COLOR] i
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
            [COLOR=darkblue]If[/COLOR] Cnt > 0 [COLOR=darkblue]Then[/COLOR]
                [COLOR=darkblue]With[/COLOR] Worksheets("Sheet3")
                    .Cells(1, NextColumn) = OleObj.Name
                    .Cells(2, NextColumn).Resize(UBound(MyArray)).Value = WorksheetFunction.Transpose(MyArray)
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                NextColumn = [COLOR=darkblue]Next[/COLOR]Column + 1
                Erase MyArray
                Cnt = 0
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    Next OleObj
    
[COLOR=darkblue]End[/COLOR] Sub
[/FONT]
 
Upvote 0
I tried this but it didn't work for me!

Actually I have 5 ActiveX list boxes in sheet1 and the indices are all in sheet3.
I put the codes which you gave in “This Workbook” and “Module 1” but the problem is that when I select the check boxes and save the file all the ActiveX list boxes and indices become cleared and disappeared.
Please help me with that.

The code shouldn't clear the listbox. When saving your workbook, it should clear the entries in Sheet3 and list the indices for the selected items in your listbox. Did you make any changes to the code? If so, can you post the exact code that you're using?
 
Upvote 0
No changes! I exactly use the same code! I sent you a message as well. could you please take a look at the workbook and see where is the problem?
Thanks!


The code shouldn't clear the listbox. When saving your workbook, it should clear the entries in Sheet3 and list the indices for the selected items in your listbox. Did you make any changes to the code? If so, can you post the exact code that you're using?
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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