Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Save as Semicolon-Delimited CSV

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To save an Excel workbook as a semicolon-delimited CSV with VBA, call Workbook.SaveAs with FileFormat:=xlCSV and then replace the comma separator with a semicolon in the resulting file, because Excel's built-in CSV format always uses the list separator from Windows regional settings, not a fixed character. The practical approach is: save to CSV normally, then read the file back with VBA and rewrite it using ; as the delimiter. This is the only reliable method that works regardless of the user's regional settings.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook with data on a single sheet, or a plan for which sheet to export.
  • Basic familiarity with opening the VBA editor (Alt+F11 on Windows, Option+F11 on Mac).
  • Write access to the folder where the CSV will be saved.

Step-by-Step

  1. Open your workbook and press Alt+F11 (Windows) or Option+F11 (Mac) to open the VBA editor. Menu labels can differ slightly by Excel version, but the editor itself is the same.

  2. In the Project Explorer, right-click your workbook name and choose Insert > Module. A blank code window appears.

  3. Paste the following procedure into the module. It saves the active sheet as a standard CSV, then rewrites the file with semicolons:

    Sub SaveAsSemicolonCSV()
        Dim ws As Worksheet
        Dim tempPath As String
        Dim finalPath As String
        Dim lineText As String
        Dim outNum As Integer
        Dim inNum As Integer
        Dim fso As Object
    
        Set ws = ActiveSheet
        tempPath = Environ("TEMP") & "\temp_export.csv"
        finalPath = Application.GetSaveAsFilename( _
            InitialFileName:=ws.Name & ".csv", _
            FileFilter:="CSV Files (*.csv), *.csv")
        If finalPath = "False" Then Exit Sub
    
        Application.DisplayAlerts = False
        ws.Copy
        ActiveWorkbook.SaveAs Filename:=tempPath, FileFormat:=xlCSV
        ActiveWorkbook.Close SaveChanges:=False
        Application.DisplayAlerts = True
    
        Set fso = CreateObject("Scripting.FileSystemObject")
        inNum = FreeFile
        Open tempPath For Input As #inNum
        outNum = FreeFile
        Open finalPath For Output As #outNum
    
        Do Until EOF(inNum)
            Line Input #inNum, lineText
            lineText = Replace(lineText, ",", ";")
            Print #outNum, lineText
        Loop
    
        Close #inNum
        Close #outNum
        fso.DeleteFile tempPath, True
        MsgBox "Saved: " & finalPath
    End Sub
  4. Press F5 to run the macro, or close the editor and assign the macro to a button on the sheet. A Save As dialog appears; choose a folder and filename.

  5. Open the resulting CSV in Notepad or a text editor to confirm the fields are separated by semicolons. Do not open it in Excel first, because Excel will re-parse it using your regional list separator and may look wrong even when the file is correct.

  6. If your data contains commas inside fields (for example, addresses or product descriptions), the simple Replace approach will corrupt those fields. In that case, skip the Replace step and instead build each line field by field from the worksheet cells, joining with ; and wrapping any field that contains a semicolon, quote, or line break in double quotes. This is more code but is the only correct way to handle embedded delimiters.

  7. For a reusable version, replace ActiveSheet with a specific sheet reference such as ThisWorkbook.Worksheets("Export"), and replace the Save As dialog with a fixed path if the macro runs unattended.

Common Mistakes

  • Assuming SaveAs can set the delimiter. The FileFormat:=xlCSV constant has no delimiter argument. Excel uses the Windows list separator, which is a comma in US English and often a semicolon in European locales. You cannot change it from VBA without changing the system setting, which affects every program.
  • Replacing commas blindly. A Replace on the whole line will also change commas that are part of the data. Only use the simple method when you know no field contains a comma.
  • Saving the original workbook instead of a copy. The code above copies the sheet to a new workbook before saving, so your source file stays open and unchanged. If you call ThisWorkbook.SaveAs directly, you convert your macro workbook into a CSV and lose the code.
  • Leaving DisplayAlerts off. If an error occurs after you set it to False and before you set it back to True, Excel will stop prompting for overwrites until restarted. Use error handling or reset it in a cleanup line.
  • Forgetting that CSV drops formatting. Only cell values are written. Formulas are saved as their calculated results, and number formats such as leading zeros or dates are lost.

Tips

  • Test with a small sample that includes a comma, a semicolon, and a quote inside a field before running on a large export.
  • Use Environ("TEMP") for the intermediate file so you do not leave stray CSVs in the user's document folder.
  • If the target program expects UTF-8, the plain text write above will not be enough. Use an ADODB.Stream object with Charset = "utf-8" to write the lines instead of the Print # statement.
  • Keep the macro in a personal workbook or an add-in so it is available across files without copying code each time.
  • Name the output with a timestamp if the macro runs on a schedule, so files are not overwritten silently.

Troubleshooting

The file still has commas. Check that the Replace line is actually running and that the final path is the one you opened. A common cause is opening the file in Excel, which displays it according to regional settings rather than showing the raw characters.

Fields are shifted or merged. Your data contains the delimiter character. Switch to the field-by-field build described in step 6 and quote fields that contain semicolons or quotes.

Runtime error 1004 on SaveAs. The temp path is invalid or the file is open elsewhere. Verify the TEMP folder exists and that no other program has the target file locked.

Accented characters appear as question marks. The default text write uses the system ANSI code page. Use the UTF-8 stream method mentioned in Tips.

Macros are blocked. Excel may disable macros from downloaded files. Save the workbook to a trusted location or adjust the Trust Center macro settings, then reopen.

Frequently Asked Questions

Can I set the delimiter directly in SaveAs?

No. The CSV file format in Excel has no delimiter parameter. The separator comes from the Windows regional list separator setting, so VBA must post-process the file or build the lines manually.

Will this work on Excel for Mac?

The SaveAs part works, but the Scripting.FileSystemObject is not available on Mac by default. Use the built-in Open and Print # statements for reading and writing, and replace the FSO delete call with the Kill statement.

Does this preserve formulas?

No. CSV stores values only. Formulas are written as their last calculated results, so recalculate before exporting if the values matter.

Why does my CSV look wrong when I reopen it in Excel?

Excel re-parses CSV using your regional list separator. A semicolon-delimited file opens correctly in locales where the separator is a semicolon, but appears as one column in locales where it is a comma. The file itself is fine; use the Text Import Wizard or open it in a text editor to verify.

Can I export more than one sheet at once?

Not with a single SaveAs call. Loop through the sheets and run the export procedure once per sheet, giving each a distinct filename.

What happens to leading zeros in part numbers?

They are lost because CSV stores values, not formats. Format the column as text before export, or prefix values with an apostrophe in the source data.