Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save each sheet in a workbook to separate CSV files

Tags:

csv

excel

vba

How do I save each sheet in an Excel workbook to separate CSV files with a macro?

I have an excel with multiple sheets and I was looking for a macro that will save each sheet to a separate CSV (comma separated file). Excel will not allow you to save all sheets to different CSV files.

like image 710
Alex Duggleby Avatar asked Sep 12 '08 14:09

Alex Duggleby


People also ask

How do I save a sheet as a different CSV file?

If not, you can check the checkbox before Worksheet name to select all sheets; (2) Check the Specify save format option; (3) Click the box below Specify save format option, and select CSV (Macintosh)(*.

Can we save multiple sheets in CSV?

You can't have multiple sheets in CSV, because CSV doesn't have sheets.


1 Answers

@AlexDuggleby: you don't need to copy the worksheets, you can save them directly. e.g.:

Public Sub SaveWorksheetsAsCsv() Dim WS As Excel.Worksheet Dim SaveToDirectory As String      SaveToDirectory = "C:\"      For Each WS In ThisWorkbook.Worksheets         WS.SaveAs SaveToDirectory & WS.Name, xlCSV     Next  End Sub 

Only potential problem is that that leaves your workbook saved as the last csv file. If you need to keep the original workbook you will need to SaveAs it.

like image 197
Graham Avatar answered Sep 20 '22 00:09

Graham