Here is slightly modified code. The code deosn't use a worksheet, just use
the VBA envirnoment to run VBA. the code opens an input and output file.
You need to modify the three lines below
Const MyPath = "C:\temp\"
Const ReadFileName = "test.txt"
Const WriteFileName = "outtest.txt"
Copy the code below as follows
1) Worksheet menu Tools - Macro - visual Basic editor
2) VBA menu Insert-Module
3) Copy code below into module window
4) You can run code either from the VBA window or excel window
From excel Tools - Macro -Macros - add_double
from VBA either select any line of code and press F5. Or from menu Run- Run
Note: you may need to change your secutiy level to medium. Macros only run
in security mode low or medium.
From worksheet menu Tools - Macro - Security
Sub add_double()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const MyPath = "C:\temp\"
Const ReadFileName = "test.txt"
Const WriteFileName = "outtest.txt"
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set fsread = CreateObject("Scripting.FileSystemObject")
Set fswrite = CreateObject("Scripting.FileSystemObject")
'open files
ReadPathName = MyPath + ReadFileName
Set fread = fsread.GetFile(ReadPathName)
Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault)
WritePathName = MyPath + WriteFileName
fswrite.CreateTextFile WritePathName
Set fwrite = fswrite.GetFile(WritePathName)
Set tswrite = fwrite.OpenAsTextStream(ForWriting, TristateUseDefault)
Do While tsread.atendofstream = False
InputLine = tsread.Readline
OutputLine = Replace(InputLine, Chr(34), Chr(34) & Chr(34))
tswrite.writeline OutputLine
Loop
tswrite.Close
tsread.Close
End Sub
Richer said:
Joel,
I lack programming savvy, how and where do I use this script?
--
Richer
Joel said:
Use filescripting method for reading and writing shown below. The Open #1
method is creating the double quotes in your file.
Sub removedouble()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const MyPath = "C:\temp\"
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set fsread = CreateObject("Scripting.FileSystemObject")
Set fswrite = CreateObject("Scripting.FileSystemObject")
ReadFileName = "test.csv"
WriteFileName = "outtest.csv"
'open files
ReadPathName = MyPath + ReadFileName
Set fread = fsread.GetFile(ReadPathName)
Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault)
WritePathName = MyPath + WriteFileName
fswrite.CreateTextFile WritePathName
Set fwrite = fswrite.GetFile(WritePathName)
Set tswrite = fwrite.OpenAsTextStream(ForWriting, TristateUseDefault)
Do While tsread.atendofstream = False
InputLine = tsread.Readline
OutputLine = Replace(InputLine, Chr(34) & Chr(34), Chr(34))
If Left(OutputLine, 1) = Chr(34) Then
OutputLine = Mid(OutputLine, 2)
End If
If Right(OutputLine, 1) = Chr(34) Then
OutputLine = Left(OutputLine, Len(OutputLine) - 1)
End If
tswrite.writeline OutputLine
Loop
tswrite.Close
tsread.Close
End Sub
:
I have a (.txt) file in notepad that has errors in it. I need to correct the
errors in excel and then save it back to the exact (.txt) format. I am using
Excel 2003 and I am having difficulties trying to accomplish this task.
Example: original file
"cellcontent1","cellcontent2","cellcontent3","cellcontent4",
Example: after corrections and saving back to (.txt)
""cellcontent1"",""cellcontent2"",""cellcontent3"",""cellcontent4"", -
[double qoutes]
or
" cellcontent1 ",'" cellcontent2 ", - [space on each
side cell content]
I must be missing a step somewhere - can someone help identify my missing
step?