VBA - Save Specific Sheets as New Workbooks with File name corresponding to C5 from each sheet.

AndrewDrewAndy

New Member
Joined
Nov 18, 2019
Messages
16
I have a workbook with 10 active files, but I only need to save sheets ("Export 1", "Export 2", "Export 3", "Export 4", "Export 5"). I have a file path I want to save them in, but I cannot find a code to save each sheets filename to the .txt in each sheets' C5 cell (each sheet has a different person's name that the file needs to be saved as). It doesn't work to change the worksheet names because the file is active and changes ofte
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
How about
Rich (BB code):
Sub AndrewDrew()
    Dim Ary As Variant
    Dim i As Long
 
    Ary = Array("Export 1", "Export 2", "Export 3")
    For i = 0 To UBound(Ary)
        Sheets(Ary(i)).Copy
        With ActiveWorkbook
            .SaveAs "C:\Mrexcel\Test\" & Range("C5").Value, 51
            .Close False
        End With
    Next i
End Sub
Change path in red to suit and if you want them as macro enabled change the blue 51 to 52
Add the other sheets to the array
 
Upvote 0
Perhaps something like this.
Code:
Sub SaveSheets()
Dim wbNew As Workbook
Dim ws As Worksheet
Dim arrSheets As Variant
Dim strFileName As String
Dim strPath As String
Dim idx As Long

    arrSheets = Array("Export 1", "Export 2", "Export 3", "Export 4", "Export 5")

    strPath = "C:\Test\"    

    For idx = LBound(arrSheets) To UBound(arrSheets)
        Set ws  = ThisWorkbook.Sheets(arrSheets(idx))
        strFileName = ws.Range("C5").Value
        ws.Copy
        Set wbNew = Application.Workbooks(Application.Workbooks.Count)
        Application.DisplayAlerts = False
        wbNew.SaveAs strPath & strFileName, FileFormat:=xlCSV

        wbNew.Close
        Application.DisplayAlerts = True
    Next idx

End Sub
Note, this code will save the sheets as CSV files - not sure if that's what you want.
 
Upvote 0
How about
Rich (BB code):
Sub AndrewDrew()
    Dim Ary As Variant
    Dim i As Long

    Ary = Array("Export 1", "Export 2", "Export 3")
    For i = 0 To UBound(Ary)
        Sheets(Ary(i)).Copy
        With ActiveWorkbook
            .SaveAs "C:\Mrexcel\Test\" & Range("C5").Value, 51
            .Close False
        End With
    Next i
End Sub
Change path in red to suit and if you want them as macro enabled change the blue 51 to 52
Add the other sheets to the array


This seemed to run fine but when I go to my updated save location there are not apparent files. Thank you
 
Upvote 0
Did you put the final \ at the end of the path?
 
Upvote 0
Oops, for some reason I thought you wanted to save the sheets as CSV files.
VBA Code:
Sub SaveSheets()
Dim wbNew As Workbook
Dim ws As Worksheet
Dim arrSheets As Variant
Dim strFileName As String
Dim strPath As String
Dim idx As Long

    arrSheets = Array("Export 1", "Export 2", "Export 3", "Export 4", "Export 5")

    strPath = "C:\Test\"   ' change path to suit here

    For idx = LBound(arrSheets) To UBound(arrSheets)

        Set ws  = ThisWorkbook.Sheets(arrSheets(idx))

        strFileName = ws.Range("C5").Value

        ws.Copy

        Set wbNew = Application.Workbooks(Application.Workbooks.Count)

        wbNew.SaveAs strPath & strFileName, FileFormat:=ThisWorkbook.FileFormat

        wbNew.Close SaveChanges:=False

    Next idx

End Sub
 
Upvote 0
How about
VBA Code:
Sub AndrewDrew()
    Dim Ary As Variant
    Dim i As Long
    Dim Pth As String
    
    Pth = Sheets("Master Data").Range("A1")
    If Not Right(Pth, 1) = "\" Then Pth = Pth & "\"
    Ary = Array("Export 1", "Export 2", "Export 4")
    For i = 0 To UBound(Ary)
        Sheets(Ary(i)).Copy
        With ActiveWorkbook
            .SaveAs Pth & Range("C5").Value, 51
            .Close False
        End With
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

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