ID: Q123183
The information in this article applies to:
Microsoft Excel does not have a menu command to automatically export data to a text file so that the text file is exported with both quotation marks and commas as delimiters. For example, there is no command to automatically
create a text file that contains the following:
"Text1","Text2","Text3"
However, you can create this functionality in Microsoft Excel by using a
Microsoft Visual Basic for Applications procedure.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/
You can use the Print # statement in a Visual Basic procedure similar to
the following to export a text file with both quotation marks and commas as
the delimiters. For the procedure to function properly, you must select the
cells that contain your data before you run it.
Before working with the example below, perform the following steps:
1. Open a new workbook.
2. Insert a new module sheet. In Excel 97 and Excel 98, on the Tools menu,
point to Macro, and then click Visual Basic Editor. In the Editor, on
the Insert menu, click Module. In Microsoft Excel versions 5.0 and 7.0,
on the Insert menu, point to Macro, and click Module.
3. Type the example macro code into the module sheet:
Sub QuoteCommaExport()
' Dimension all variables.
Dim DestFile As String
Dim FileNum As Integer
Dim ColumnCount As Integer
Dim RowCount As Integer
' Prompt user for destination file name.
DestFile = InputBox("Enter the destination filename" _
& Chr(10) & "(with complete path):", "Quote-Comma Exporter")
' Obtain next free file handle number.
FileNum = FreeFile()
' Turn error checking off.
On Error Resume Next
' Attempt to open destination file for output.
Open DestFile For Output As #FileNum
' If an error occurs report it and end.
If Err <> 0 Then
MsgBox "Cannot open filename " & DestFile
End
End If
' Turn error checking on.
On Error GoTo 0
' Loop for each row in selection.
For RowCount = 1 To Selection.Rows.Count
' Loop for each column in selection.
For ColumnCount = 1 To Selection.Columns.Count
' Write current cell's text to file with quotation marks.
Print #FileNum, """" & Selection.Cells(RowCount, _
ColumnCount).Text & """";
' Check if cell is in last column.
If ColumnCount = Selection.Columns.Count Then
' If so, then write a blank line.
Print #FileNum,
Else
' Otherwise, write a comma.
Print #FileNum, ",";
End If
' Start next iteration of ColumnCount loop.
Next ColumnCount
' Start next iteration of RowCount loop.
Next RowCount
' Close destination file.
Close #FileNum
End Sub
4. Before running the macro, select the data you want to export then run
the QuoteCommaExport subroutine.
For additional information, please see the following article in the
Microsoft Knowledge Base:
ARTICLE-ID: Q103985
TITLE : XL4: Macro to Export Text File with Comma AND Quote
Delimiters
Additional query words: 7.00 5.00 5.00c quotes csv save numbers
Keywords : kbprg kbdta kbdtacode PgmHowto PgmFilem KbVBA
Version : WINDOWS:5.0,5.0c,7.0,97; MACINTOSH:5.0,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto
Last Reviewed: May 17, 1999