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!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Could you give a clearer example?

How do you get that result from just this?

0 1 0 5 1 5 1 etc...
 
Upvote 0
You don't actually get the result persay. If it see's that it knows to Paste a set that I give it. I.E. the application doesn't assign values to the lines in it's initial run - it just does teh first line. But if you replace that first line with the second group it defines 3 more functions in the app...
 
Upvote 0
I'm sorry, perhaps it's just me, but you've totally lost me now.:)
 
Upvote 0
Ok - hmm. Let's say a text file has a bunch of data in it for, say, 5 different cases; Case 1, Case 2... For each Case the file contains 10 lines of info. What I would like to do is find that 10th line (which I know what it says) and replace it with something that is 3 lines long.

That make any sense?
 
Upvote 0
Becoming a bit clearer.

What you would need to do is read the text file in line by line, compare the line to whatever (I'm still a bit unclear what that that is).

Based on the comparison you would write the line to another file and if needed also write the new data.

You could then delete the original file and rename the new one.

All this is possible, in fact not too difficult, buit more information is needed.

I'll try and mock something up in code and post back.
 
Upvote 0
I just want to look to see (essentially) if a string of text is equal to another string of text that I define. If that is true then write a third string into the new file. Getting any better?
 
Upvote 0
Here's a little something I mocked up.

It looks through a text file, writing to a new text file and if it finds 5 it also writes 3 new pieces of data.
Code:
Sub test()
Dim FF1, FF2
Dim strLine As String
Dim I As Long
    FF1 = FreeFile()
    FF2 = FreeFile(1)
    
    Open "C:\Papers\TextTest.txt" For Input As FF1
    Open "C:\Papers\NewTextTest.txt" For Output As FF2
        While Not EOF(FF1)
            Line Input #FF1, strLine
            Print #FF2, strLine
            If Val(strLine) = 5 Then
                For I = 1 To 3
                    Print #FF2, "New Data"
                Next I
            End If
        Wend
    Close #FF2
    Close #FF1
End Sub
 
Upvote 0
Is there a way to do the pop up window deal - that lets you navigate and select the file you want to read in?
 
Upvote 0
Code:
Sub test()
Dim FF1, FF2
Dim strLine As String
Dim strFileName
Dim I As Long

    FF1 = FreeFile()
    FF2 = FreeFile(1)
    
    strFileName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    
    If strFileName = False Then Exit Sub
    
    Open strFileName For Input As FF1
    Open "C:\Papers\NewTextTest.txt" For Output As FF2
        While Not EOF(FF1)
            Line Input #FF1, strLine
            Print #FF2, strLine
            If Val(strLine) = 5 Then
                For I = 1 To 3
                    Print #FF2, "New Data"
                Next I
            End If
        Wend
    Close #FF2
    Close #FF1
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,129
Messages
6,129,049
Members
449,482
Latest member
al mugheen

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