user3651785
user3651785

Reputation: 21

How can I modify a text file using VBA and maintaing the format of the existing text lines

How can I modify a text file using VBA, without losing the the format of my text lines - I'm talking about lines which have at least 1 number on it For example: If I have 5 numbers on a line, in the new modified file, all of those number are on 5 rows. The lines which contains characters other than numbers are fine!

Please, if you have 3-4 minutes, open any single text file using VBA (containing a line with at least 2 numbers on it) and try to save it to a new file, using the method posted below.

Open Path For Input As #1 
Do Until EOF(1)     
Input #1, ReadData
File.WriteLine (ReadData) 

The problem :The format of the lines with numbers is not preserved.

Original:

1 2 3 4 5 6

Modified:

1
2
3
4
5
6

Any suggestions?

Upvotes: 1

Views: 705

Answers (2)

Jimmy Smith
Jimmy Smith

Reputation: 2451

Input is designed to assign values to individual variables. Like a record of comma-separated values.

Try Line Input,

Open Path For Input As #1 
Do Until EOF(1)     
Line Input #1, ReadData
File.WriteLine (ReadData) 

more info here

Upvotes: 0

user783388
user783388

Reputation:

Writeline writes a line (with a line feed). You will have to assemle a full line and then write it into a file:

dim s as string
s=data1 & " " & data2 & " " '...
file.writeline(s)

Upvotes: 1

Related Questions