Excelgoodies logo +1 650 491 3131

 

Merge text files using VBA

The below code merges around 50 ascii files into one It uses File System Object and you need to add a reference of Microsoft Scripting Runtime

 

Sub Append_Text_Files()

Dim F1 As FileSystemObject
Dim F2 As FileSystemObject

Dim oTS As TextStream
Dim oTS1 As TextStream

Dim vTemp

Set F1 = New FileSystemObject
Set F2 = New FileSystemObject

For i1 = 1 To 50

Set oTS = F1.OpenTextFile(“c:\Sheet” ; i1 ; “.txt”, ForReading)
vTemp = oTS.ReadAll

Set oTS1 = F1.OpenTextFile(“c:\CombinedTemp.txt”,
ForAppending, True)
oTS1.Write (vTemp)

Next i1

End Sub

Explore comprehensive Excel VBA Macro Course here and, automate your routine Excel Reports, tasks and processes.

 

Happy Excelling
Team Excelgoodies

VBA & Python