Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save individual Excel sheets as CSV

Tags:

excel

vba

I need to parse Excel work sheets. Now I save each individual work sheet as .csv and it works great. I use OpenCSV to parse the files etc. but to create those .csv files are a pain.

What would be the easiest and quickest way to save individual work sheets as .csv in Excel? I am assuming some kind of VBA macro would do the job, but since I am not a VBA programmer I have no idea how to do it. Maybe I can just record a macro somehow?

like image 357
Marthinus Avatar asked Sep 07 '11 09:09

Marthinus


People also ask

How do I save multiple Excel sheets as CSV?

(1) Keep selecting all sheets. 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)(*.


2 Answers

Very roughly,

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    ws.SaveAs "C:\docs\" & ws.Name & ".csv", xlCSV
Next

This does not include any error coding nor does it make allowances for sheet names that will lead to illegal file names. It all depends on how robust you need the whole thing to be.

like image 199
Fionnuala Avatar answered Sep 22 '22 14:09

Fionnuala


As a first answer, here is the code used to save a file to CSV:

ActiveWorkbook.SaveAs "C:\Marthinus.csv", fileformat:=6

More info about SaveAs

like image 29
JMax Avatar answered Sep 24 '22 14:09

JMax