Quotes in CSV Files
I changed two lines in the variable declaration (marked "Change") and added 4
new lines (marked "New", however one of the lines takes up 2 lines. In VBA
the underscore _ is the line continuation character). The NG often wraps
things funny, so hopefully you will be able to sort it out.
It creates a temporary file using the original's name plus "Temp". Then
when it is done it deletes the original data file and renames the temporary
file to the original data filename.
Sub test2()
Dim strFileIn As String '<Change
Dim strFileOut As String '<Change
Dim FSO As Scripting.FileSystemObject
Dim fDest As Scripting.TextStream
Dim strData As String
Dim lngFNum1 As Long
strFileIn = Application.GetOpenFilename '<NEW
strFileOut = Replace(strFileIn, ".csv", _
"Temp.csv", 1, 1, vbTextCompare) '<NEW
Set FSO = New Scripting.FileSystemObject
Set fDest = FSO.CreateTextFile(strFileOut, True)
lngFNum1 = FreeFile()
Open strFileIn For Input As #lngFNum1
Do While Not EOF(lngFNum1)
Line Input #lngFNum1, strData
strData = Replace(strData, """", "", 1, -1, vbTextCompare)
fDest.WriteLine strData
Loop
Close
fDest.Close
Kill strFileIn '<NEW
Name strFileOut As strFileIn '<NEW
End Sub
"Help?" wrote:
Okay, I've got it working. But I have one problem. I need the below to except
variable filenames and keep that file name consistant. Right now I have the
filename pulling from columns in the excel for bothe csv and excel pieces
that is automatically saved. How can I make the below to except variable
filename?
Const strFileIn As String = "I:\Excel\CSVData.csv"
Const strFileOut As String = "I:\Excel\CSVClean.csv"
"JMB" wrote:
This appeared to work. I've found TextStreamObjects will write the data to
file w/o enclosing it in quotes. You'll need to test to see if it works for
what you are doing. You will need to set up a reference to the Scripting
library (Tools/References - Microsoft Scripting Runtime). Change strFileIn
and out for your filenames.
To check, I changed the extension on the CSVClean file to .txt so it would
open in Notepad. I'm sure you know that if you open the cleaned file in
Excel, the (,) will split the data into two columns.
Sub test2()
Const strFileIn As String = "I:\Excel\CSVData.csv"
Const strFileOut As String = "I:\Excel\CSVClean.csv"
Dim FSO As Scripting.FileSystemObject
Dim fDest As Scripting.TextStream
Dim strData As String
Dim lngFNum1 As Long
Set FSO = New Scripting.FileSystemObject
Set fDest = FSO.CreateTextFile(strFileOut, True)
lngFNum1 = FreeFile()
Open strFileIn For Input As #lngFNum1
Do While Not EOF(lngFNum1)
Line Input #lngFNum1, strData
strData = Replace(strData, """", "", 1, -1, vbTextCompare)
fDest.WriteLine strData
Loop
Close
fDest.Close
End Sub
"Help?" wrote:
I am trying to find a macro that will force the "" around the below to not
happen. Because the below has a comma in it, on csv convertion it
automatically insert the "". Can I force this through VBA macro to not do
this? This will be the third time I have asked for help, but not received
yet. Can anyone help me in this?
Coming out as:
"%!
XGF
9999 SETBUFSIZE
(,) SETDBSEP
QSTRIP_on
(GuntherIPEP.jdt) SETJDT
(IPEP.dbm) STARTDBM"
I need it to be:
%!
XGF
9999 SETBUFSIZE
(,) SETDBSEP
QSTRIP_on
(GuntherIPEP.jdt) SETJDT
(IPEP.dbm) STARTDBM
|