Save Split Excel Data as CSV Files Using VBA
Question details
Save each workbook containing split Excel data as a CSV file using VBA.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A VBA macro splits Excel data into separate workbooks that need to be exported as CSV files.
- Observed behavior
- The saved files are not in CSV format when SaveAs does not specify a CSV filename and file format.
Save the workbook containing the macro so ThisWorkbook.Path points to an existing folder. Identify the workbook and worksheet created by your split routine, and make sure the output folder is writable.
Save Each Split Workbook with the CSV File Format
Use this approach when your VBA routine creates a separate workbook for each portion of the data.
The example assumes splitWb refers to the workbook created by your split routine and Z identifies the current output file. CSV saves only the active worksheet, so activate the worksheet you intend to export.
SaveAs changes the split workbook to CSV format. CSV cannot retain additional worksheets, formatting, or macros.
In Excel, open Developer > Visual Basic and locate the procedure that creates the split workbooks.
After creating each split workbook, use the following code, replacing splitWb and Z with your routine's workbook variable and file counter as needed: With splitWb .Worksheets(1).Activate .SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & "file-" & Z & ".csv", FileFormat:=xlCSV End With
Run the macro from Developer > Macros, then open the folder containing the macro workbook and verify that the file-Z.csv outputs contain the intended data.

Work with CSV Files in WPS Spreadsheets
WPS Office is a free, lightweight alternative for opening and editing spreadsheets in a familiar interface. Its Excel format compatibility makes moving files straightforward, though it does not fix an Excel VBA macro.

Frequently Asked Questions
Why do I need both a .csv extension and xlCSV?
The .csv extension gives the output file the expected name, while FileFormat:=xlCSV tells Excel to write the file in CSV format. Specify both in the workbook's SaveAs call.
Which worksheet does Excel export to CSV?
Excel exports the active worksheet. Before calling SaveAs, activate the intended sheet, for example with splitWb.Worksheets("Data").Activate.
Where will the example save the CSV files?
ThisWorkbook.Path points to the folder containing the workbook that holds the macro. Save that workbook first, or replace ThisWorkbook.Path with another existing output folder.
How can I save UTF-8 CSV files instead?
In Excel versions that support it, replace FileFormat:=xlCSV with FileFormat:=xlCSVUTF8 in the SaveAs command. Check the exported file in the application that will import it.




