VBA let user choose multiple sheets in workbook from list and copy to new workbook

Want2BExcel

Board Regular
Joined
Nov 24, 2021
Messages
112
Office Version
  1. 2016
Platform
  1. Windows
I found this answer from JohnnyL in this post VBA - Allow User to Select Which Worksheet to Copy
Its's almost what I need 🤌
VBA Code:
Sub ChooseSheetNumberToUse()                                                                        ' select a sheet to copy
'
    Dim UserSelectedSheet       As String
'
    Worksheets(1).Activate                                                                          ' Had to add this to allow the sheet selected to work
'
    MsgBox "In the next popup window, Click the sheet name " & vbCrLf & "that you would like to copy." ' Advise user what to do next
'
    If Application.CommandBars("workbook tabs").Controls(16).Caption Like "More Sheets*" Then       ' This CommandBars only shows up to 16 sheets, after that it
'                                                                                                       will display the first 15 sheets and the 16th item will be
'                                                                                                       an option of "More Sheets..." which will give full list
        Application.CommandBars("workbook tabs").Controls(16).Execute                               ' If more than 16 sheets are found, click the "More Sheets ..."
    Else                                                                                            ' Otherwise ...
        Application.CommandBars("Workbook Tabs").ShowPopup 800, 300                                 '   Display the sheets found
    End If
'
    UserSelectedSheet = ActiveSheet.Name                                                            ' Save the sheet name that the user selected
    Debug.Print UserSelectedSheet                                                                   ' Test print, for verification, name of user selected sheet
'
'   The variable 'UserSelectedSheet' now contains the sheet name selected by the user
'
'
'   Add desired additional coding here ...
'
End Sub
Is it possible to choose multiple sheets and not only one from Application.CommandBars("Workbook Tabs").ShowPopup 800, 300? And then save them all to a new workbook? The new workbook should stay active. So it easy to save it immediately to a path.
....well it could be cool to write that path in the code also....along with filename based on a fixed string like "Schedule" and the value in two cells B1 and E1 😎
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Create a userform an insert a listbox and button
Just place the Listbox1 on the userform, don't do anything with it. The code will populate the listbox.

1660403810321.png


place this code in the userform module.
VBA Code:
Private Sub CommandButton1_Click()
    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).Copy
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    With ListBox1
        For sh = 1 To Sheets.Count
            .AddItem ActiveWorkbook.Sheets(sh).Name
        Next sh
        .MultiSelect = 1
    End With
End Sub

When the userform shows, select the sheets you want in a new workbook and click the button.
 
Upvote 0
Solution
Create a userform an insert a listbox and button
Just place the Listbox1 on the userform, don't do anything with it. The code will populate the listbox.

View attachment 71516

place this code in the userform module.
VBA Code:
Private Sub CommandButton1_Click()
    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).Copy
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    With ListBox1
        For sh = 1 To Sheets.Count
            .AddItem ActiveWorkbook.Sheets(sh).Name
        Next sh
        .MultiSelect = 1
    End With
End Sub

When the userform shows, select the sheets you want in a new workbook and click the button.
IT's BEAUTIFUL 🤩 Took me a while, to figure out everything regarding creating and implementing a userform. This was my first...but definitely not my last 🥲
Thank you davesexcel 🙏
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,395
Members
449,081
Latest member
JAMES KECULAH

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