Yes. In VBA, open the workbook with Workbooks.Open, then call SaveAs with FileFormat:=xlCSVUTF8 to create a UTF-8 CSV file. Excel exports only the active worksheet, so choose the sheet you need before saving.
What the VBA export does
A CSV file is a plain-text representation of rows and columns, not a complete Excel workbook. When Excel saves a workbook as CSV, it writes the active sheet’s cell contents to the new file. It does not carry over other worksheets, formulas as formulas, charts, formatting, macros, or workbook features. Formula cells are represented by their calculated results, not by the formula text.
The example below opens an existing workbook, saves its active sheet as UTF-8 CSV, and closes the converted workbook. It writes the CSV to a separate path, leaving the original Excel file in place. Change both paths to real locations on your computer before running it.
VBA code to open a workbook and save as CSV
Sub ExportWorkbookAsCsv()
Dim sourcePath As String
Dim outputPath As String
Dim wb As Workbook
Dim previousAlerts As Boolean
Dim errorMessage As String
' Change these paths to match your files.
sourcePath = "C:\Data\source.xlsx"
outputPath = "C:\Data\export.csv"
If Len(Dir$(sourcePath)) = 0 Then
MsgBox "Source workbook not found: " & sourcePath, vbExclamation
Exit Sub
End If
If StrComp(sourcePath, outputPath, vbTextCompare) = 0 Then
MsgBox "Choose a different path for the CSV output.", vbExclamation
Exit Sub
End If
previousAlerts = Application.DisplayAlerts
On Error GoTo Failed
Application.DisplayAlerts = False
Set wb = Workbooks.Open(Filename:=sourcePath)
wb.SaveAs Filename:=outputPath, _
FileFormat:=xlCSVUTF8, _
CreateBackup:=False, _
Local:=True
wb.Close SaveChanges:=False
Application.DisplayAlerts = previousAlerts
MsgBox "CSV saved to: " & outputPath, vbInformation
Exit Sub
Failed:
errorMessage = Err.Description
On Error Resume Next
If Not wb Is Nothing Then wb.Close SaveChanges:=False
Application.DisplayAlerts = previousAlerts
On Error GoTo 0
MsgBox "CSV export failed: " & errorMessage, vbExclamation
End Sub
The xlCSVUTF8 file format is intended to produce a UTF-8 CSV and is suitable for current Excel for Microsoft 365 on Windows. The Local:=True argument tells Excel to use local language and regional settings when saving. Those settings can affect details such as how numbers and separators are represented, so inspect the output if another system expects a particular CSV convention.
Where to put the macro and how to run it
Put the code in a macro-enabled workbook that will not itself be converted to CSV. For example, create a separate controller workbook and save it as an Excel Macro-Enabled Workbook (.xlsm), or store the procedure in your Personal Macro Workbook. If you put the macro in the source workbook and then save that workbook as CSV, the CSV cannot retain the VBA project.
- Save a copy of the workbook you intend to process. Macros cannot be undone with Ctrl+Z, so a backup gives you a safe way to recover if the wrong file or sheet is used.
- Open the macro-enabled controller workbook. In Excel for Windows, press Alt+F11 to open the Visual Basic Editor. On Mac, use the Visual Basic command on the Developer tab; the keyboard shortcut can vary with the keyboard and function-key settings.
- In the editor, choose Insert > Module, paste in the procedure, and edit
sourcePathandoutputPath. Use full paths, including the file names and extensions. - Run
ExportWorkbookAsCsvfrom the editor, or return to Excel and run it from the macro list. If prompted, allow macros only for files you trust.
For example, if the source is C:\Reports\June.xlsx and the output should be C:\Reports\June.csv, set those exact strings in the two path variables. The source and destination can be in different folders. Make sure the destination folder already exists; this procedure does not create folders.
Choosing which worksheet gets exported
Excel’s workbook-level SaveAs exports the active worksheet when the target format is CSV. A workbook may open with the sheet that was active when it was last saved, which might not be the one you intend. If a particular sheet must always be exported, activate it after opening the workbook and before calling SaveAs:
Set wb = Workbooks.Open(Filename:=sourcePath)
wb.Worksheets("Data").Activate
wb.SaveAs Filename:=outputPath, FileFormat:=xlCSVUTF8
Replace Data with the worksheet’s exact tab name. The sheet must exist and be visible for this straightforward approach. If the name is misspelled, VBA raises an error; the error handler in the full macro closes the workbook and restores Excel’s alert setting. When using a sheet-specific line, add it to the full procedure before its SaveAs statement.
Another common requirement is exporting several sheets. A single CSV cannot store multiple worksheets as separate tabs. Run the export separately for each sheet, using a distinct output filename for each one. A loop can automate that process, but filenames should be planned carefully so that one sheet does not overwrite another export.
What appears in the CSV
CSV is a data exchange format, not a way to preserve the workbook’s appearance. Excel does not include cell colors, font styles, column widths, merged-cell behavior, conditional formatting, filters, or formulas as working formulas. A recipient opening the CSV in Excel will see a new worksheet containing the text and values in the file; the original workbook’s layout is not recreated.
Pay special attention to dates, decimal numbers, and identifiers. A date may be written in a form influenced by its cell display and local settings. A number such as an account code can lose leading zeros if Excel treats it as a number rather than text. Long numeric identifiers can also be changed or displayed in scientific notation when a CSV is opened in a spreadsheet application. If exact text matters, make the source values text before exporting and check the resulting file in a plain-text editor.
Blank cells and empty rows may be represented differently from how they look in the worksheet. A CSV does not preserve Excel’s used range as a designed table with formatting. Before export, remove stray content far below or to the right of the intended data, and confirm that the sheet has the headers and rows the receiving system expects.
CSV encoding, commas, and regional settings
The example uses xlCSVUTF8 so text can be saved in UTF-8, which is useful when data contains characters beyond basic English letters, such as accented names. Encoding is separate from the delimiter: choosing UTF-8 does not guarantee that every receiving program will interpret the file’s columns in the same way.
CSV commonly uses commas between fields, but Excel’s regional settings can influence the separator used when saving or opening delimited data. In some locales, a semicolon may be used where a comma is expected because the comma is also used as a decimal separator. The Local argument in the example asks Excel to use local settings. If an import system requires a specific delimiter, verify the output with that system’s instructions rather than assuming the file will use a comma. A CSV format setting and a text encoding setting solve different problems.
Overwrite prompts, errors, and safe handling
The procedure temporarily sets Application.DisplayAlerts to False. This suppresses Excel’s normal save-format and overwrite prompts, which lets the macro run without waiting for a click. It also means an existing file at outputPath can be replaced without a confirmation dialog. Choose a unique destination or check that the existing file is safe to overwrite before running the macro.
The code stores the previous alert setting and restores it after success or failure. Its error handler also attempts to close the opened workbook without saving further changes. This matters because a failed save should not leave the workbook open unexpectedly or leave Excel’s alerts disabled for later work. Error handling cannot make a bad path valid, however: confirm that the source exists, the destination folder is writable, and the CSV is not locked by another application.
Keep the macro’s paths under your control and do not enable VBA in an unknown workbook just to export data. A macro can make changes beyond saving a file. Run trusted code from a workbook you recognize, and preserve a backup of important source data.
When to use SaveAs and when to use another method
Use SaveAs when you need a quick export of one worksheet and its current cell contents. It uses Excel’s own file conversion and is usually the simplest option for a repeatable, single-sheet task. It is not a good fit if you need to preserve formulas, styles, multiple sheets, or an exact custom schema in one output file.
If the recipient needs a full workbook, save or distribute an .xlsx file instead. If a recurring export must control column order, quoting, delimiters, blank-value rules, or data transformations, consider building a dedicated export routine that writes each field deliberately, or use the receiving application’s supported import/export tool. For a one-off task, Excel’s File > Save As interface can save a sheet as CSV without VBA, but it still has the same basic limitation: CSV represents one sheet of data rather than a whole workbook.
Frequently Asked Questions
Does VBA save every sheet in the workbook to CSV?
No. A CSV file contains one worksheet’s data. Excel saves the active sheet, so activate the intended sheet first or run separate exports with separate filenames.
Does saving as CSV change or delete my original XLSX file?
With different source and output paths, the macro creates a separate CSV and closes the converted workbook. The original file remains at its source path. Do not set the output path to the source file’s path.
Can I keep formulas in the CSV?
CSV does not preserve Excel formulas as formulas. Exported formula cells contain their calculated results. If the recipient needs formulas and workbook behavior, use an Excel workbook format instead.
Why does my CSV show semicolons instead of commas?
Excel’s regional settings can affect the separator used for CSV output. Check the file in a text editor and check the import system’s requirements. UTF-8 controls text encoding; it does not by itself force a particular delimiter.
Can I export a worksheet as CSV on Mac?
Excel for Mac also supports VBA and CSV saving in the desktop application, though paths use Mac-style locations and the VBA editor shortcut can differ by keyboard settings. This code is written for current desktop Excel and may need path changes on Mac; it does not run in Excel for the web, which cannot run VBA macros.
Related
- Task List Template Excel Spreadsheet: Set Up and Use
- Tax Audit Form 3CD in Excel: Format and Filing Guide
- Supplier Contact List Template in Excel: Fields and Setup
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA INDEX: Return Values from a RangeIn Excel VBA, “INDEX” usually means Excel’s INDEX worksheet function, called as Application.WorksheetFunction.Index(...) or Application.Index(...) to return an…
- Excel VBA Macro Recorder: How It Works and When to Use ItThe Excel VBA macro recorder turns many actions you perform in a workbook into VBA code that you…
- Excel VBA Dynamic Worksheet Names: How They WorkIn Excel VBA, make a worksheet name dynamic by storing the desired tab name in a string variable…
- Excel VBA Find a Substring in a StringIn Excel VBA, use InStr to find a substring inside a string: it returns the position of the…
- Loop Through a Named Range in VBA: Practical ExamplesTo loop through every cell in an Excel named range in VBA, assign the range to a Range…
- Excel Power Query and VBA: Differences, Uses, and ChoiceExcel Power Query and VBA solve different problems: use Power Query to connect to data and repeatably import,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.