Save specific sheets in a workbook as separate files using VBA

ubergreen

New Member
Joined
Jun 13, 2024
Messages
33
Office Version
  1. 2021
I found the code below and modified it to fit my needs, but the last step that I haven't been able to figure out is how to have it save only certain or specific sheets in my workbook. At the moment, it saves all of the sheets in the workbook.

VBA Code:
   Public Sub SaveSheetsAsCsv()

   Dim WS As Excel.Worksheet
   Dim SaveToDirectory As String

   Dim CurrentWorkbook As String
   Dim CurrentFormat As Long
  
   Application.DisplayAlerts = False

   CurrentWorkbook = ThisWorkbook.FullName
   CurrentFormat = ThisWorkbook.FileFormat
  
   SaveToDirectory = "(folder location)"
  
   For Each WS In ThisWorkbook.Worksheets
  
   WS.SaveAs SaveToDirectory & WS.Name, xlCSV
   Next


End Sub
 
For clarification, do you want to save each sheet as a separate file?

Yea. I would like to save each sheet as a separate file. However, I want to only save certain/specific sheets.

For example, if my workbook has Sheet1, Sheet2, and Sheet3. I want to be able to have it only save Sheet1 and Sheet3 as a csv file when the code is ran.
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try:
VBA Code:
Public Sub SaveSheetsAsCsv()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim WS As Worksheet, SaveToDirectory As String, fName As String
    SaveToDirectory = "(folder location)"
    For Each WS In Sheets(Array("Sheet1", "Sheet3"))
        WS.Copy
        fName = SaveToDirectory & WS.Name & ".csv"
        With ActiveWorkbook
            .SaveAs Filename:=fName, FileFormat:=xlCSV, CreateBackup:=False
            .Close , False
        End With
    Next WS
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try:
VBA Code:
Public Sub SaveSheetsAsCsv()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim WS As Worksheet, SaveToDirectory As String, fName As String
    SaveToDirectory = "(folder location)"
    For Each WS In Sheets(Array("Sheet1", "Sheet3"))
        WS.Copy
        fName = SaveToDirectory & WS.Name & ".csv"
        With ActiveWorkbook
            .SaveAs Filename:=fName, FileFormat:=xlCSV, CreateBackup:=False
            .Close , False
        End With
    Next WS
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

Thanks. That did it. Surprisingly, while trying to find this solution, it was more common for people to want to simply save all of the sheets in a workbook as separate files and not only specific/select sheets.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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