Set default selections on a List Box on Workbook_Open()

sparky2205

Well-known Member
Joined
Feb 6, 2013
Messages
507
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi folks,
I have a listbox that populates with a list of worksheet names when the workbook is opened.
The user then selects worksheets from the list to have visible or hidden.
What I'm trying to achieve is when the workbook is closed an reopened the users last selections in the listbox are retained.
This is the code I've been trying:
VBA Code:
Private Sub Workbook_Open()

Dim N As Long
    For N = 2 To ActiveWorkbook.Sheets.Count - 1
        Sheets("Setup").ListBox1.AddItem ActiveWorkbook.Sheets(N).Name
        If ActiveWorkbook.Sheets(N).Visible = False Then
            Sheets("Setup").ListBox1.List(N).Selected = True
        End If
    Next N
    Sheets("Setup").ListBox1.Height = Sheets("Setup").ListBox1.ListCount * 12

End Sub
It returns the error: "Could not get the List property. Invalid property array index."
The code works fine without the IF statement, i.e. the ListBox is populated correctly with the list of worksheet names.
I think I'm missing an array here somewhere but I'm not sure how to implement it.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
First, the Selected property belongs to the ListBox object. Secondly, the indexing for the Selected property starts at zero. Therefore, try the following instead...

VBA Code:
Private Sub Workbook_Open()

Dim N As Long
    For N = 2 To ActiveWorkbook.Sheets.Count - 1
        Sheets("Setup").ListBox1.AddItem ActiveWorkbook.Sheets(N).Name
        If ActiveWorkbook.Sheets(N).Visible = False Then
            With Sheets("Setup").ListBox1
                .Selected(.ListCount - 1) = True
            End With
        End If
    Next N
    Sheets("Setup").ListBox1.Height = Sheets("Setup").ListBox1.ListCount * 12

End Sub

However, your macro can be re-written as follows...

VBA Code:
Private Sub Workbook_Open()

    Dim N As Long
 
    With ThisWorkbook
        For N = 2 To .Sheets.Count - 1
            .Sheets("Setup").ListBox1.AddItem .Sheets(N).Name
            If .Sheets(N).Visible = False Then
                With .Sheets("Setup").ListBox1
                    .Selected(.ListCount - 1) = True
                End With
            End If
        Next N
    End With
 
   
    With ThisWorkbook.Sheets("Setup").ListBox1
        .Height = .ListCount * 12
    End With

End Sub

Hope this helps!
 
Upvote 0
Solution
Good morning Domenic,
apologies for the tardy response, I was off work for a few days.
Your solution works beautifully.
And thank you for this "First, the Selected property belongs to the ListBox object. Secondly, the indexing for the Selected property starts at zero."
All these little nuggets of explanation are very valuable to me.
 
Upvote 0
That's great, I'm happy to help.

And thanks for your feedback.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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