Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier 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

by Allen Wyatt
(last updated April 26, 2014)


When using the tools available from the ribbon to export a worksheet, as a CSV file, 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 & ";"
                TempString = TempString & _
                  Cells(i, j).Value
            End If
        Cells(i, 1).Value = TempString

    Application.CutCopyMode = False

    ActiveWorkbook.SaveAs Filename:=FName & ".txt", _
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:

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

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9243) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel 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. ...


Keeping Callouts Positioned

Using graphics to add callouts to your graphics is a common occurrence in Word. Here's how to stop all those graphics from ...

Discover More

Determining if Num Lock is On

Need to know if the Num Lock key is on or off? You can use a short bit of macro code to figure out the state of the key.

Discover More

Determining Month Names for a Range of Dates

Given a starting date and an ending date, you may want to generate the names of all the months between those two dates. This ...

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 (ribbon)

Conditionally Displaying a Message Box

You can, from within your macros, easily display a message box containing a message of your choice. If you want to display ...

Discover More

Working while a Macro is Running

If you have a macro that takes a long time to process a workbook, you might want to continue working in Excel while the macro ...

Discover More

Using InputBox to Get Data

Need your macro to get some input from a user? The standard way to do this is with the InputBox function, described in this ...

Discover More

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

View most recent newsletter.


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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. 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 6 - 3?

2015-02-07 02:57:07


I want to export my table data into a csv file using excel automation in my programming.

I also want the double quote (") as a Field Start/End Delimiter and | as Field Separator.

how to get this ?

2015-01-28 07:55:04


As for the comment "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.":

I can rebut: My regional settings has for years had ";" as list separator, yet xlCSV yields ","s.

2014-04-29 08:45:50


Oh. My. Goodness. There are so many terrible things in this macro. It clearly was written pre-2007 and never updated.

* You haven't declared ANY of your variables. There's simply no reason to skip this step.
* As Jeff C mentioned, you are assuming that the ONLY extension available is .xls. Either of his two solutions would work, depending on your desired outcome
* The Range("B65000").End(xlUp).Row is an antiquated function that could have failed even in xl2003 and before: it's based on the assumption that there's no data beyond row 65000, but xl2003 had 65,536 rows, and xl2007+ has 1,048,576 rows! Plus, if there's data in row 65,000, then you've added new ways for the call to fail.
* Similarly, the "HA1" references aren't correct either. The last column in xl2003 was IV, not HA, and the last row in xl2007 is XFD. Plus, I don't understand why (1) you would only reference the first row in determining how wide your data is, and (2) why you call the exact same function once for every row in your data, when you could save it to a variable and reference the variable instead. (Ok, I thought about it, and perhaps (1) is because you want to write null/blanks into your data, and you are assuming that there's a column header for each column containing data).
* Both the width/height could utilize the Worksheet.UsedRange object, or a myriad of other ways to find the height and width that don't rely on hard-coded cell values. Heck, why not just use For Each rng in UsedRange, then instead of having to have i and j to keep track of the row and column, you just have your rng variable which keeps track of it all at once.
* This part isn't *so* terrible, but I think instead of checking on every row if you have reached the last column, I find it easier to just add the separator at every iteration of the loop then remove the last separator after the loop.
* Speaking of separators, the whole purpose was to be able to define your own. This should at a minimum be saved as a constant at the top instead of a string literal buried within the code.
* Columns(1).Select / Selection.Copy... really? Columns(1).Copy
* Workbooks.Add returns a workbook object, so you can say With Workbooks.Add and not rely on a hope and a prayer using ActiveSheet and ActiveWorkbook
* You forgot to delete the column at the end.

Really, the whole concept of inserting a column is pretty silly. It makes so much more sense to read the rows into a string variable then write the whole thing to a text file. Or if you don't want to worry about the I/O functions (I always forget how they work... one of these days I'll write a class to encapsulate them all so I don't *have* to remember how they work), save each row to a slot in a variant array, plop the variant array into your new workbook.

2014-04-28 09:11:12

Jeff C

The code to get FName is a bit outdated since the extension for the active workbook name could be different (xlsx, xlsm, etc.). You can always parse the name based on the last period character found, but why not just omit this code altogether and just append ".txt" to the name (MyWorkbook.xlsx.txt)?

This Site

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

Newest Tips

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.