Modifying text files with VBA?

D

Dave Parker

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
 
B

Bernie Deitrick

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
 
K

kkknie

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

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

Top