Written by Allen Wyatt (last updated November 30, 2018)
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:
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.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
When you import a CSV file into an Excel worksheet, you may be surprised at how the program allocates the information ...
Discover MoreIf you have links in your workbook to data in other workbooks, you may want to control whether Excel updates those links ...
Discover MoreWhen you import information from a CSV text file, Excel formats the data according to its default settings. Wouldn't it ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-05-05 10:05:37
Benjamin Conboy
when I open up the text file, there are a whole bunch of printing characters, specifying colors, etc, can I get just the text?
2014-03-17 10:40:10
Scott Renz
Thanks Allen,
I have a problem though. Sometimes people put semi-colons and or double-quotation marks into their descriptions that go into some of the cells in the worksheet. When I open up that semi-colon separted file in Excel, some of the rows are truncated with the trailing parts after the truncation put into subsequent rows.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments