Data Import & Export

Save Split Excel Data as CSV Files Using VBA

Chanuka GeekiyanageChanuka Geekiyanage Nov 18, 2026 870 views

Question details

Save each workbook containing split Excel data as a CSV file using VBA.

Save Split Excel Data as CSV Files 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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, open Developer > Visual Basic and locate the procedure that creates the split workbooks.

2
Add the CSV save command

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

3
Run and check the export

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.

Save Each Split Workbook with the CSV File Format
Choose the correct worksheet: Replace Worksheets(1) with the appropriate sheet name or index if the data to export is on another worksheet. Excel may prompt you about CSV format limitations or an existing filename.
Free Microsoft Office alternative

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.

Open and save CSV files for data exchangeCompatible with Microsoft Excel formats, including XLSX and XLSUse a familiar spreadsheet interface to review exported dataMove existing spreadsheet files into a lightweight office suite
microsoft office alternative - wps office

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.