Convert an Excel XLSM Worksheet to CSV with a VBA Macro
Question details
Export the active worksheet from an XLSM workbook to a CSV file without changing the original workbook.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The user wants a VBA macro to copy the active worksheet and save the copy as a CSV file with a generated filename.
- Observed behavior
- The desired result is a separate CSV file containing the active worksheet's data while the original XLSM file remains unchanged.
Open the XLSM file in desktop Excel and select the worksheet you want to export. Make sure macros are enabled for this trusted workbook and that you can access the VBA editor.
Export the Active Worksheet with a VBA Macro
Use this macro to create a separate CSV file with a timestamped default name while leaving the original XLSM file unchanged.
In Excel, open Developer > Visual Basic, or press Alt+F11 on Windows. In the VBA editor, select the XLSM workbook in the Project pane.
Choose Insert > Module and paste this code into the module: Sub ActiveSheet_To_CSV() Dim ws As Worksheet Dim wbCopy As Workbook Dim fileName As Variant Dim errorMessage As String Set ws = ActiveSheet fileName = Application.GetSaveAsFilename( _ InitialFileName:="tmp_" & Format(Now, "yyyymmdd_hhnnss") & ".csv", _ FileFilter:="CSV Files (*.csv), *.csv") If fileName = False Then Exit Sub On Error GoTo ExportError ws.Copy Set wbCopy = ActiveWorkbook wbCopy.SaveAs Filename:=CStr(fileName), FileFormat:=xlCSV, Local:=True wbCopy.Close SaveChanges:=False Exit Sub ExportError: errorMessage = Err.Description If Not wbCopy Is Nothing Then wbCopy.Close SaveChanges:=False MsgBox "CSV export did not complete: " & errorMessage, vbExclamation End Sub
Return to Excel, select the worksheet to export, and choose Developer > Macros. Select ActiveSheet_To_CSV, click Run, then choose a location and confirm or edit the generated filename.

Save a Worksheet Copy as CSV Without a Macro
Use Excel's Save As command for a one-time export when automation is not needed.
Export an XLSM Worksheet to CSV in WPS Spreadsheets
Use WPS Spreadsheets to open the XLSM file and save the selected worksheet as a separate CSV file without running a VBA macro. The original XLSM file remains unchanged on disk.
- 1. Open the workbook: Open WPS Spreadsheets and use Menu > Open to select the XLSM file.
- 2. Select the worksheet: Click the tab of the worksheet you want to export.
- 3. Save as CSV: Choose Menu > Save As, select a location, enter a new filename, and select CSV as the file type. Confirm any prompts about saving only the current sheet or losing unsupported features.
- 4. Check the export: Open the saved CSV file and verify its data. Use the original XLSM file when you need the workbook's macros or other sheets.

Frequently Asked Questions
Does the VBA macro change the original XLSM file?
No. The macro uses ws.Copy to create a new workbook and saves that copy as CSV. Unsaved changes in the active sheet can appear in the CSV, but the macro does not save them to the original XLSM file.
Can I choose where the CSV file is saved?
Yes. The macro opens a Save As dialog with a timestamped default filename. Choose a folder and edit the filename before confirming.
Will the CSV contain formulas and formatting?
No. CSV stores the exported data as text, including displayed results rather than usable formulas. Formatting, macros, and other worksheets are not retained.
Why does the CSV use a different delimiter than expected?
The macro saves with xlCSV and Local:=True, so Excel can use local system settings for the delimiter. Check the resulting file and the importing application's delimiter settings.




