Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default 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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default 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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Large text file toby131 Excel Discussion (Misc queries) 4 September 28th 09 10:53 PM
why is my simple text file so large (22 kb) jbt Excel Discussion (Misc queries) 1 May 8th 07 05:22 PM
Large file sizes after modifying spreadsheet in Excel 2007. Artagel Excel Discussion (Misc queries) 3 April 5th 07 03:34 PM
Modifying large CSV files Mike Excel Discussion (Misc queries) 9 March 9th 07 12:15 AM
Importing a large text file hmm Excel Discussion (Misc queries) 4 March 15th 06 03:13 PM


All times are GMT +1. The time now is 02:01 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"