Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

read in a text file in line by line parse it and write to a new file to save as a CSV

I need to read in a text file one line at a time as I read it in I will parse it and then write it again to a new file to be saved as as CSV.

What would be the simplest way to do this??

  • 2
1 Solution
Dim lngReadFileNum As Long
Dim lngWriteFileNum As Long
Dim strBuffer As String

lngReadFileNum = FreeFile
lngWriteFileNum = FreeFile

Open "C:\path\file.txt" For Input As #lngReadFileNum
Open "C:\path\newfile.csv" For Output As #lngWriteFileNum

    Do Until EOF(lngReadFileNum)
        Line Input #lngReadFileNum, strBuffer
        'parse data in strBuffer

        'write to new file
        Print #lngWriteFileNum, "Some New Data"

Close #lngWriteFileNum
Close #lngReadFileNum
MaritimerAuthor Commented:
This may be a dumb question but I seem to be getting an error on the second line:

Open "C:\path\newfile.csv" For Output As #lngWriteFileNum


File already open.
Any ideas???
You should be opening different files.  The first 'Open' statement should be opening the file you are reading data in from, and the second 'Open' should specify the new file to be written to.  These two files must be different.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now