ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Modifying a LARGE text file (https://www.excelbanter.com/excel-programming/417538-modifying-large-text-file.html)

Fabian Grodek

Modifying a LARGE text file
 
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.
Fabian

Dave Peterson

Modifying a LARGE text file
 
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
Loop

Close #1
Close #2

End Sub

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

Fabian Grodek wrote:

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.
Fabian


--

Dave Peterson

Fabian Grodek

Modifying a LARGE text file
 
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!
Fabian

"Dave Peterson" wrote:

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
Loop

Close #1
Close #2

End Sub

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

Fabian Grodek wrote:

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.
Fabian


--

Dave Peterson



All times are GMT +1. The time now is 09:16 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com