Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy selected worksheets to new workbook via form 1

Status
Not open for further replies.

DatabaseDude

Programmer
Nov 7, 2005
112
US
I have a userform with a listbox containing all worksheets in the current workbook.

My goal is to allow users to select worksheets from the list, and for these selected sheets to be copied to a new workbook.

When recording a macro of this process, I find the following:

Code:
Sheets(Array("Sheet 1","Sheet 2","Sheet 3")).Select
Sheets(Array("Sheet 1","Sheet 2","Sheet 3")).Copy

Of course, I'd simplify it as so:

Code:
Sheets(Array("Sheet 1","Sheet 2","Sheet 3")).Copy

Now ... how do I insert the selected worksheets from the listbox control into this code segment?

Thanks in advance,
Bryant
 
Bryant,

Here is the only way I could get this to work with the Sheets(Array) construct. The following code example could be placed into the Click event of a CommandButton on your Userform. It assumes a ListBox named lstSheets from which the user selects the sheets to copy:
Code:
Dim vSheets As Variant
Dim sSelected As String
Dim i As Long

   With lstSheets
     sSelected = ""
     For i = 0 To .ListCount - 1
       If .Selected(i) Then
         sSelected = sSelected & "," & .List(i)
       End If
     Next i
   End With
   
   sSelected = Mid$(sSelected, 2)
   vSheets = Split(sSelected, ",")
   ThisWorkbook.Sheets(vSheets).Copy

Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top