How to get excel to export quotes always...

R

Rich DeVito

Hi-

I need excel to export ASCII text data with quotes around each field, not
just fields that contain a comma. Our data needs to be shipped to a program
that always expects the quotes.

Excel only puts a quote around a field with a comma in it. I want excel to
do it always?

Thanks,
 
J

Joel

You need a macro

Sub WriteCSV()
Const myFileName = "c:\temp\myfile.csv"
Const ForReading = 1, ForWriting = 2, _
ForAppending = 3

Set fs = CreateObject("Scripting.FileSystemObject")

FileSaveName = Application.GetSaveAsFilename( _
fileFilter:="Text Files (*.CSV), *.CSV")
If FileSaveName = False Then
MsgBox ("Cannot Get Filename - Exiting Macro")
Exit Sub
End If


Set f = fs.CreateTextFile _
(Filename:=FileSaveName, overwrite:=True)

Lastrow = Range("A" & Rows.Count).End(xlUp).Row

For RowCount = 1 To Lastrow

LastCol = Cells(RowCount, Columns.Count).End(xlToLeft).Columns
If LastCol = 1 And _
Range("A" & RowCount) = "" Then
'if row is empty write blank line
f.writeline
Else
OutputLine = ""

For ColCount = 1 To LastCol
If ColCount = 1 Then
OutputLine = Chr(34) & Cells(RowCount, ColCount) & Chr(34)
Else
OutputLine = OutputLine & "," & _
Chr(34) & Cells(RowCount, ColCount) & Chr(34)
End If
Next ColCount
f.writeline OutputLine
End If
Next RowCount
f.Close
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top