Macro to copy certain sheets to a new workbook, then save the workbook, naming it the worksheet name

diygail123

New Member
Joined
Oct 24, 2018
Messages
25
Hi guys

I am trying to write a macro that will copy a worksheet, then save it using the worksheet name as the filename. I then want it to go to other worksheets, in the same workbook, and do the same. I was trying to use a list of names typed on a worksheet (List of Names) to tell the macro which worksheets to copy, but Im not having much luck! I need the macro to scroll through said worksheets, creating a copy, and saving, using the worksheet name as a filename. See below for as far as I have got. Dont laugh, Im a beginner! The save isnt working, its naming it as the sheet I started on (called Macro), and I dont know how to get it to loop through the names on the list

Sub CopySave2()


'DECLARE VARIABLES
Dim StrFileName As String
StrFileName = ActiveSheet.Name & ".xlsx"


Dim ListOfNames As Range, c As Range
Set ListOfNames = Worksheets("List of Names").Range("b2:b20") 'BRIAN KRAL IS THE FIRST WORKSHEET I WANT TO COPY BUT I DONT KNOW HOW TO PUT THIS IN THE CODE WITHOUT STATING THE NAME!


'For Each c??


'MAKE A COPY OF FIRST WORKSHEET ON THE LIST, TO A NEW WORKBOOK
Sheets("BRIAN KRAL").Copy
'ADD TWO WORKSHEETS AND NAME THEM "DETAILS" & "PIVOT"
Sheets.Add After:=ActiveSheet
Sheets.Add After:=ActiveSheet
Sheets("Sheet3").Name = "Details"
Sheets("Sheet2").Name = "Pivot"

'SAVE THE WORKBOOK
ActiveWorkbook.SaveAs StrFileName

Next c
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Code:
Dim Sh As Worksheet
For Each c In ListOfNames
    For Each Sh In Worksheets
        If Sh.Name = c.Value Then
             Sh.copy
             Sheets.Add After:=ActiveSheet
             ActiveSheet.Name = "Details"
             Sheets.Add After:=ActiveSheet
             ActiveSheet.Name = "Pivot"
        End If
    Next Sh
Next c
 
Last edited:
Upvote 0
Code:
Sub CopySave2()
'DECLARE VARIABLES
Dim colShts As New Collection
Dim StrFileName As String, sName As String
Dim ListOfNames As Range, c As Range
Dim i As Integer


'Set ListOfNames = Worksheets("List of Names").Range("b2:b20") 'BRIAN KRAL IS THE FIRST WORKSHEET I WANT TO COPY BUT I DONT KNOW HOW TO PUT THIS IN THE CODE WITHOUT STATING THE NAME!


   'collect only items not null
Sheets("List of Names").Activate
Range("b2").Select
While ActiveCell.Value <> ""
  colShts.Add ActiveCell.Value
  ActiveCell.Offset(1, 0).Select 'next row
Wend


For i = 1 To colShts.Count
     sName = colShts(i)
     
          'MAKE A COPY OF FIRST WORKSHEET ON THE LIST, TO A NEW WORKBOOK
    Sheets(sName).Select
    StrFileName = ActiveSheet.Name & ".xlsx"


    Sheets(sName).Copy
    ActiveWorkbook.SaveAs Filename:=StrFileName, FileFormat:=xlOpenXMLWorkbook
    ActiveWindow.Close
Next
Set colShts = Nothing
End Sub
 
Upvote 0
Another option
Code:
Sub CopySave2()
   Dim Cl As Range

   For Each Cl In Sheets("List of Names").Range("b2:b20")
      Sheets(Cl.Value).Copy
      With ActiveWorkbook
         .Sheets.Add.Name = "Details"
         .Sheets.Add.Name = "Pivot"
         .SaveAs ThisWorkbook.Path & "\" & Cl.Value & ".xlsx", 51
         .Close False
      End With
   Next Cl
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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