Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Specifying a Delimiter when Saving a CSV File in a Macro.

Specifying a Delimiter when Saving a CSV File in a Macro

Written by Allen Wyatt (last updated September 16, 2023)
This tip applies to Excel 97, 2000, 2002, and 2003


When creating a CSV file using the menus to export a worksheet, Arkadiusz noted that he can specify that he wants to use a semicolon (;) as a field delimiter. However, if he saves a CSV file using a macro (FileFormat:=xlCSV or xlCSVWindows), then he cannot specify a semicolon as a delimiter.

This works this way by design in VBA. The Excel implementation of the export routines for VBA always use whatever the Windows regional settings are to determine how items in a CSV should be separated. Specifically, the routine looks at the List Separator field for the delimiter. This means that you can, if desired, change the delimiter to a semicolon by changing the List Separator setting in your regional settings configuration.

If you don't want to change the regional settings, then you can instead write your own macro that will output the file in any way you desire. Consider, for a moment, the following macro, which will output the file:

Sub CreateFile()
    FName = ActiveWorkbook.Name
    If Right(FName, 4) = ".xls" Then
        FName = Mid(FName, 1, Len(FName) - 4)
    End If

    Columns(1).Insert Shift:=xlToRight

    For i = 1 To Range("B65000").End(xlUp).Row
        TempString = ""
        For j = 2 To Range("HA1").End(xlToLeft).Column
            If j <> Range("HA1").End(xlToLeft).Column Then
                TempString = TempString & _
                  Cells(i, j).Value & ";"
            Else
                TempString = TempString & _
                  Cells(i, j).Value
            End If
        Next
        Cells(i, 1).Value = TempString
    Next

    Columns(1).Select
    Selection.Copy
    Workbooks.Add
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False

    ActiveWorkbook.SaveAs Filename:=FName & ".txt", _
      FileFormat:=xlPrinter
End Sub

This macro takes a unique approach to creating the output file. What it does is to insert a column at the left of your worksheet, and then concatenates all the data to the right of that column into the newly inserted column A. It adds a semicolon between each field. Once that is done, it grabs the information it put into column A and writes it into a new workbook. This workbook is then saved to disk using the xlPrinter file format, which means that it is put out "as is" without any modification whatsoever.

If you prefer a more direct approach, writing the information directly to a file without making changes to your worksheet, take a look at the macro at this blog post:

https://web.archive.org/web/20060302021412/http:/www.dicks-blog.com:80/archives/2004/11/09/roll-your-own-csv/

The macro uses commas between each field, but it can be easily modified so that it uses semicolons instead.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3232) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Specifying a Delimiter when Saving a CSV File in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Ignoring Case in a Comparison

Do you want Excel to take the case of your text into account when it does comparisons in a formula? The IF statement ...

Discover More

Empty PivotTable Cells Don't Show as Blank

You can configure Excel so that it displays special text within blank PivotTable cells. Here's how to make the changes ...

Discover More

Endnotes in a Separate Document

When you add endnotes to a document they are normally positioned (as one would expect) at the end of the document. You ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (menu)

Saving Information in a Text File

The VBA programming language provide with Excel allows you to create and modify text files quite easily. Here's how to ...

Discover More

Pulling Filenames into a Worksheet

You can use Excel for all types of data processing. You may want to work with filenames in a worksheet, but the first ...

Discover More

Saving in Two Locations

When you save a workbook to disk, you may want to automatically save a duplicate workbook in a separate location. This ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.