Modifying a LARGE text file


Fabian Grodek

I have a large (15000 lines) text file. I want to write a VBA macro to modify
one specific line in that file, say line number 10, replacing the existing
text with some string/values taken from some cell.
Any hint will be greately appreciated.

Dave Peterson

Option Explicit
Sub testme01()

Dim TextLine As String
Dim rCtr As Long
Dim myStr As String
Dim WhichRecord As Long

WhichRecord = 10
myStr = Worksheets("Sheet1").Range("A1").Text

'just in case
Close #1
Close #2

'my test files
Open "c:\my documents\excel\book2.txt" For Input As #1
Open "c:\my documents\excel\book2.txt.out" For Output As #2

rCtr = 0
Do While Not EOF(1)
Line Input #1, TextLine
rCtr = rCtr + 1

If rCtr = WhichRecord Then
TextLine = TextLine & myStr
End If

Print #2, TextLine

Close #1
Close #2

End Sub

I just appended the string, but you could parse it anyway you like.

Fabian Grodek

Thank you Dave. That did the job! I thought it would be slow due to the large
number of lines, but the result was immediate!

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question