Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Modifying text files with VBA?

I have two (ASCII) text files (FileA and FileB), and I need to replace
a portion of FileA with the contents of FileB. The position of the
text in the files is fixed - I just need to replace lines 274-326 in
FileA with lines 1-53 of FileB.

I've tried a subroutine that reads both files to a workbook, merges
the required portion, then saves a text formatted file. However,
portions of the files are space delimited and others are comma
delimited so this method doesn't work properly. I think I need to
directly access and modify the files from VBA (of course, I'm open to
other suggestions).

Thank you for any help you can provide.
Dave
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Modifying text files with VBA?

Dave,

Try the macro below, which assumes that the files have a TXT extension. You
will be asked to choose the files.

HTH,
Bernie
MS Excel MVP

Option Explicit
Sub ReplaceSpecificLines()
Dim ReadStr As String
Dim FileName1 As String
Dim FileName2 As String
Dim OrigFNum As Integer
Dim RepFNum As Integer
Dim FileNumOut As Integer
Dim Counter As Double

Counter = 1

FileName1 = Application.GetOpenFilename( _
"Text Files (*.txt),*.txt", _
, "Pick the Original File")
If FileName1 = "" Then End

FileName2 = Application.GetOpenFilename( _
"Text Files (*.txt),*.txt", _
, "Pick the Replacement File")
If FileName2 = "" Then End

OrigFNum = FreeFile()
Open FileName1 For Input As #OrigFNum
RepFNum = FreeFile()
Open FileName2 For Input As #RepFNum
FileNumOut = FreeFile()
Open "Merged File.txt" For Output Access Write As #FileNumOut


Do While Seek(OrigFNum) <= LOF(OrigFNum) And Counter < 274
Application.StatusBar = "Processing line " & Counter
Line Input #OrigFNum, ReadStr
Print #FileNumOut, ReadStr
Counter = Counter + 1
Loop
Do While Seek(RepFNum) <= LOF(RepFNum) And Counter < 327
Application.StatusBar = "Processing line " & Counter
Line Input #RepFNum, ReadStr
Print #FileNumOut, ReadStr
Line Input #OrigFNum, ReadStr
Counter = Counter + 1
Loop
Do While Seek(OrigFNum) <= LOF(OrigFNum)
Counter = Counter + 1
Application.StatusBar = "Processing line " & Counter
Line Input #OrigFNum, ReadStr
Print #FileNumOut, ReadStr
Loop

Close #OrigFNum
Close #RepFNum
Close #FileNumOut

Application.StatusBar = False

End Sub

"Dave Parker" wrote in message
om...
I have two (ASCII) text files (FileA and FileB), and I need to replace
a portion of FileA with the contents of FileB. The position of the
text in the files is fixed - I just need to replace lines 274-326 in
FileA with lines 1-53 of FileB.

I've tried a subroutine that reads both files to a workbook, merges
the required portion, then saves a text formatted file. However,
portions of the files are space delimited and others are comma
delimited so this method doesn't work properly. I think I need to
directly access and modify the files from VBA (of course, I'm open to
other suggestions).

Thank you for any help you can provide.
Dave



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Modifying text files with VBA?

This may not be exactly what you want, but it should be a start

Code
-------------------
Sub bubb()

'I have two (ASCII) text files (FileA and FileB), and I need to replace
'a portion of FileA with the contents of FileB. The position of the
'text in the files is fixed - I just need to replace lines 274-326 in
'FileA with lines 1-53 of FileB.

Dim strLine1 As String
Dim strLine2 As String
Dim strLines(10000) As String
Dim i As Long
Dim iMax As Long
Dim bFound As Boolean

Close
Open "c:\file1.txt" For Input As #1
i = 0
bFound = False
Do Until EOF(1)
i = i + 1
Line Input #1, strLine1
If i < 274 Or i 326 Then
strLines(i) = strLine1
Else
If bFound = False Then
bFound = True
Open "c:\file2.txt" For Input As #2
Do Until EOF(2)
Line Input #2, strLine2
strLines(i) = strLine2
Loop
Close #2
End If
End If
Close #1
iMax = i

Open "c:\file3.txt" For Output As #3

For i = 1 To iMax
Print #3, strLines(i)
Next

Close #3

End Su
-------------------




--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Modifying text files with VBA?

Thank you very much - the code works great!

Dave

"Bernie Deitrick" <deitbe @ consumer dot org wrote in message ...
Dave,

Try the macro below, which assumes that the files have a TXT extension. You
will be asked to choose the files.

HTH,
Bernie
MS Excel MVP

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
Excel (xls) files spontaneously converting to text files Be-jamin Excel Discussion (Misc queries) 0 November 18th 08 05:31 PM
modifying files created from older versions of Excel Patriot Excel Discussion (Misc queries) 0 August 13th 08 09:51 PM
Am getting sharing violation message when modifying excel files Carol Ann Excel Discussion (Misc queries) 0 May 5th 08 11:31 AM
Modifying large CSV files Mike Excel Discussion (Misc queries) 9 March 9th 07 12:15 AM
Modifying text in Chart Tips Ashley[_3_] Excel Programming 1 January 5th 04 05:57 PM


All times are GMT +1. The time now is 04:48 AM.

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"