Excel VBA: Save as CSV UTF-8 (Step-by-Step)
In this article
To save an Excel workbook as a UTF-8 CSV using VBA, use the SaveAs method with FileFormat:=xlCSVUTF8. That constant exists in Excel 2016 and later; on older versions you must write the file manually with ADODB.Stream or Scripting.FileSystemObject. The one-line answer is ActiveWorkbook.SaveAs "C:\path\file.csv", FileFormat:=xlCSVUTF8, but the details matter: which workbook, which sheet, whether formulas or values are written, and whether you are overwriting an open file.
What You'll Need
- Excel 2016 or later (including Microsoft 365) for the built-in
xlCSVUTF8file format. Excel 2013 and earlier do not have this constant. - A workbook open in Excel with the data you want to export, and the VBA editor available (Alt+F11 on Windows, or the Developer tab).
- Write access to the destination folder. If the folder is synced by OneDrive or SharePoint, the file may be locked while syncing.
- Macro security set to allow macros, or the file saved as .xlsm. If macros are blocked, the code will not run at all.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or Fn+Alt+F11 (Mac). In the Project pane, right-click your workbook and choose Insert > Module. A blank module opens on the right.
- Write the basic export macro. Paste this into the module, changing the path to a real folder on your machine:
TheSub SaveAsCsvUtf8() Dim ws As Worksheet Dim savePath As String Set ws = ThisWorkbook.Worksheets("Sheet1") savePath = "C:\Temp\export.csv" ws.Copy ActiveWorkbook.SaveAs Filename:=savePath, _ FileFormat:=xlCSVUTF8, _ Local:=False ActiveWorkbook.Close SaveChanges:=False End Subws.Copyline creates a temporary workbook containing only that sheet, so you do not export the whole file. If you want the whole workbook, replacews.CopywithThisWorkbook.Activateand useActiveWorkbook.SaveAsdirectly. - Set the file format correctly.
xlCSVUTF8is the constant that writes a comma-separated file with a UTF-8 byte-order mark. If you usexlCSVinstead, Excel writes ANSI text and non-ASCII characters (accents, currency symbols, Chinese, Arabic) will be corrupted when opened elsewhere. Always usexlCSVUTF8when the data contains anything outside plain ASCII. - Decide what gets written.
SaveAswrites the current values of the sheet. If a cell contains a formula, the formula result is saved, not the formula itself. If you need the formula text, copy the range and usePasteSpecial xlPasteValuesinto a helper sheet first, then export that sheet. - Handle the overwrite prompt. If the target file already exists, Excel normally asks whether to overwrite. To suppress the prompt, add
Application.DisplayAlerts = FalsebeforeSaveAsand set it back toTrueafterwards. Do not leave alerts disabled for the rest of the session. - Run the macro. Press F5 with the cursor inside the subroutine, or assign it to a button on the sheet. Watch the Immediate window (Ctrl+G) for errors. If the file was created, open it in Notepad or a text editor to confirm the characters look right; a UTF-8 file with a BOM will usually show a small marker at the start in some editors, which is normal.
- Close and restore. The temporary workbook created by
ws.Copymust be closed without saving, as shown. If you skip that, Excel leaves an extra unsaved workbook open each time the macro runs.
Common Mistakes
- Using
xlCSVinstead ofxlCSVUTF8. The file opens fine in Excel but breaks in other tools when it contains non-ASCII text. - Saving the original workbook instead of a copy. If you call
ThisWorkbook.SaveAswith a .csv filename, Excel converts the open workbook to CSV and you lose formatting, formulas and multiple sheets. Always copy the sheet to a temporary workbook first. - Forgetting to close the temporary workbook, leaving orphaned windows open.
- Assuming the BOM is always wanted. Some import tools treat the BOM as part of the first column name. If your downstream system chokes on it, you need a manual UTF-8 writer rather than
xlCSVUTF8. - Hard-coding a path that does not exist.
SaveAsfails if the folder is missing; create it withMkDiror check withDirfirst. - Leaving
Application.DisplayAlerts = Falseon after an error, which hides later prompts.
Tips
- Use
ThisWorkbookrather thanActiveWorkbookso the macro always targets the file it lives in, even if the user has switched windows. - Build the path with
Environ("USERPROFILE")orThisWorkbook.Pathso the macro works on other machines. - If you need a specific delimiter (semicolon, tab),
xlCSVUTF8always uses a comma. For other delimiters, write the file line by line withADODB.Streamand setCharset = "utf-8". - Test with a sheet containing accented characters such as é, ü and ñ, plus a currency symbol, before relying on the macro for real exports.
- For repeated exports, wrap the code in a loop over worksheets and name each output file after the sheet name.
Troubleshooting
If the macro errors with "Sub or Function not defined" on xlCSVUTF8, your Excel version is older than 2016 or the reference is missing. Check Help > About in Excel to confirm the version. On older versions, either upgrade or write the file with ADODB.Stream.
If the file saves but characters appear as question marks or garbled symbols in another program, the receiving program is probably reading it as ANSI. Open the file in a text editor that reports encoding and confirm it is UTF-8. If it is, the problem is on the import side, not in Excel.
If you get "Method 'SaveAs' of object '_Workbook' failed", the path is invalid, the folder is read-only, or the file is open in another program. Close the other program, check permissions, and try a simple path like C:\Temp\test.csv.
If the macro runs but the CSV is empty, the sheet you copied may be hidden or the range may be filtered. Unhide the sheet and clear filters before exporting.
If Excel prompts "A file named ... already exists" every time, add the DisplayAlerts toggle described above, or delete the target file with Kill before saving (only if you are sure it is safe to overwrite).
Frequently Asked Questions
Does xlCSVUTF8 work on Excel 2013?
No. The constant was added in Excel 2016. On Excel 2013 and earlier, write the file manually with ADODB.Stream set to UTF-8, or export as xlCSV and accept that non-ASCII characters may not survive.
Will the CSV include formulas or values?
Values. SaveAs writes the calculated result of each formula, not the formula text. If you need the formulas, copy them as text into a helper column first.
How do I save only one sheet, not the whole workbook?
Copy the sheet to a new workbook with ws.Copy, then call SaveAs on the new ActiveWorkbook and close it without saving. This is the pattern shown in the step-by-step above.
Why does my CSV start with strange characters?
That is the UTF-8 byte-order mark. xlCSVUTF8 writes it so that Excel and many other programs detect the encoding correctly. Some import tools treat it as part of the first header, so strip it on the import side if needed.
Can I choose a different delimiter?
Not with xlCSVUTF8; it always uses a comma. For semicolon or tab-delimited UTF-8 output, write the file line by line with ADODB.Stream and set the charset to utf-8.
Does this work on Mac Excel?
Yes, xlCSVUTF8 is supported in Excel 2016 and later on Mac. Paths use the Mac format, for example /Users/name/Documents/export.csv, and the folder must already exist.