Reading/Changing a text file

bigmc6000

New Member
Joined
Aug 17, 2005
Messages
47
This is really a macro-guru question. I have a text file that is read by another app. In it's text file it reads:

0 1 0 5 1 5 1 etc...

I would like to replace that with:


0 0 1 5 1 5 1
0 1 0 1 2 4
0 3 4 5 1 1
etc...

Is there a cleaver way to read in the file, search for the first section of text and replace it with the second section of text and do that for the whole file?

Thanks in advance!
 
Maybe this will help. I'm running into a type mismatch problem at the If statement. Here is the line of text I"m looking for in the file...

" 105 010 111000001 P0.1 0 1 2004 "

I thought I could define it as Line1 and define the lines to write as Write1, Write2, etc...
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Can you post the actual code you are using?
 
Upvote 0
Code:
Sub test()
Dim FF1, FF2
Dim strLine As String
Dim I As Long
Dim Line1 As String
Dim Line2 As String
Dim Line3 As String
Dim Line4 As String
Dim Line5 As String

FF1 = FreeFile()
FF2 = FreeFile(1)

Line1 = " 105           010           111000001     P0.1          0             1             2004 "
Line2 = " 0.4           0             0             0             0             0             0             0             0 "
Line3 = " 0.04          0             0             0             0             0             0             0             0 "
Line4 = " 000           3             1 "
Line5 = " 20            0 "

strFileName = Application.GetOpenFilename("Poppi Files (*.pdb), *.pdb")
   
If strFileName = False Then Exit Sub
   
Open strFileName For Input As FF1

strFileName2 = Application.GetOpenFilename("Poppi Files (*.pdb), *.pdb")
   
If strFileName2 = False Then Exit Sub
   
Open strFileName2 For Output As FF2

While Not EOF(FF1)
Line Input #FF1, strLine
Print #FF2, strLine
If Val(strLine) = Line1 Then
Print #FF2, Line2
Print #FF2, Line3
Print #FF2, Line4
Print #FF2, Line5

End If
Wend
Close #FF2
Close #FF1
End Sub

I know I need to change the "Val(strline) to something to check for the string rather than the value but I don't know what.

Thanks!

I added a lookup for the output as well...
 
Upvote 0
Also, I've got another line that I'd like to replace rather than just add lines into the file - is there an easy way to do that?
 
Upvote 0
You might just be able to remove the Val() around strLine.
 
Upvote 0

Forum statistics

Threads
1,215,943
Messages
6,127,820
Members
449,409
Latest member
katiecolorado

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top