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
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
What are those certain sheets?
 
Upvote 0
Try:
VBA Code:
Public Sub SaveSheetsAsCsv()
    Application.ScreenUpdating = False
    Dim WS As Worksheet, SaveToDirectory As String
    Application.DisplayAlerts = False
    SaveToDirectory = "(folder location)"
    For Each WS In Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
        WS.SaveAs SaveToDirectory & WS.Name, xlCSV
    Next WS
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Public Sub SaveSheetsAsCsv()
    Application.ScreenUpdating = False
    Dim WS As Worksheet, SaveToDirectory As String
    Application.DisplayAlerts = False
    SaveToDirectory = "(folder location)"
    For Each WS In Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
        WS.SaveAs SaveToDirectory & WS.Name, xlCSV
    Next WS
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

Thanks, that worked. Just a few questions so I can better understand what you did for my own future reference.

I am still fairly new to excel/vba, so my questions may be a bit basic.

I saw that you changed the dim WS from Excel.Worksheet to just Worksheet. Is there any difference to using either one?

I also saw that you added Applicaton.DisplayAlerts = False at the start and then used = True at the end. Is there a reason for this? I thought you would want to simply leave it as False and only need to have it used in the beginning?
 
Upvote 0
Regarding your first question, there is no difference. It is just a shorter form. If you are referring to Application.Screenupdating=False, this prevents screen refreshing so it speeds up the macro. You should set it to True at the end to restore screen refreshing. Generally, when you set a command to False, you should re-set it to True at the appropriate time. I hope this makes sense.
 
Upvote 0
Regarding your first question, there is no difference. It is just a shorter form. If you are referring to Application.Screenupdating=False, this prevents screen refreshing so it speeds up the macro. You should set it to True at the end to restore screen refreshing. Generally, when you set a command to False, you should re-set it to True at the appropriate time. I hope this makes sense.

Ah, got it. For the True/False, I was referring the below that's bolded.

Public Sub SaveSheetsAsCsv()
Application.ScreenUpdating = False
Dim WS As Worksheet, SaveToDirectory As String
Application.DisplayAlerts = False
SaveToDirectory = "(folder location)"
For Each WS In Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
WS.SaveAs SaveToDirectory & WS.Name, xlCSV
Next WS
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0
That line of code prevents alert messages from popping up and should be used with caution because if the macro errors out, you won’t get any error messages. If possible, error checking should be included in the code instead of using that line. In any case, it should be set to True at some point.
 
Upvote 0
Try:
VBA Code:
Public Sub SaveSheetsAsCsv()
    Application.ScreenUpdating = False
    Dim WS As Worksheet, SaveToDirectory As String
    Application.DisplayAlerts = False
    SaveToDirectory = "(folder location)"
    For Each WS In Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
        WS.SaveAs SaveToDirectory & WS.Name, xlCSV
    Next WS
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

Wanted to revisit this, since it seems like I initially missed this issue or for some reason I have a minor typo somewhere that's causing me issues.

It seems like with this code, it saves everything as expected, except it is using the data/values from only the first sheet for all of the files that are being saved with the code.
 
Upvote 0
For clarification, do you want to save each sheet as a separate file?
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,118
Members
453,021
Latest member
Justyna P

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