Select multiple Sheets based on Userform Listbox selections

blaker

New Member
Joined
Jul 1, 2013
Messages
32
Office Version
  1. 365
Platform
  1. Windows
I have a listbox in a userform, I have code to populate all the sheets in the workbook in the listbox, looking for code to select multiple sheets in the workbook based on the selected sheet names in the listbox.
The code I have so far will only select one of the sheets selected. see pic
1611102309945.png

here is the code I have:

Dim i As Integer, sht As String
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
sht = ListBox1.List(i)
End If
Next i
Sheets(sht).Activate

any help greatly appreciated!!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about
VBA Code:
    Dim i As Integer, sht As String, arr() As String, n As Long
    
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) = True Then
            ReDim Preserve arr(n)
            arr(n) = ListBox1.List(i)
            n = n + 1
        End If
    Next i
    Sheets(arr).Select
 
Upvote 0
Solution
You are welcome and thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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