Microsoft KB Archive/213448

= XL2000: Procedure to Export a Text File with Both Comma and Quote Delimiters =

PSS ID Number: 213448

Article Last Modified on 11/3/2003

-

The information in this article applies to:


 * Microsoft Excel 2000
 * Microsoft Visual Basic for Applications

-



This article was previously published under Q213448



For a Microsoft Excel 2002 version of this article, see 291296.

For a Microsoft Excel 98 and earlier version of this article, see 123183.



SUMMARY
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.



MORE INFORMATION
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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

http://www.microsoft.com/partner/referral/

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

You can use the Print # statement in a Visual Basic procedure similar to the following one 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, follow these steps:  Open a new workbook. On the Tools menu, point to Macro, and then click Visual Basic Editor (or simply press ALT+F11). In the Visual Basic Editor, click Module on the Insert menu.  Type or paste the following sample code in 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  Before running the macro, select the data that you want to export, and then run the QuoteCommaExport subroutine.

Additional query words: quotes csv delimited save numbers XL2000

Keywords: kbdtacode kbhowto kbinfo kbProgramming KB213448

Technology: kbExcel2000 kbExcel2000Search kbExcelSearch kbExcelWinSearch kbVBASearch kbZNotKeyword3 kbZNotKeyword6

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© 2004 Microsoft Corporation. All rights reserved.