Hi all,
I have the following VBA code to copy the content of one text file to another text file. However I want to do that for fifteen files using a loop.
Public Sub CombineTextFiles()
Const csA As String = "G:\Test.txt" 'A File Path
Const csB As String = "G:\Test2.txt" 'B File Path
Dim bCheck As Boolean
Dim iFile As Integer
Dim sBContent As String, sStrComb As String, sBefore As String, sAfter As String
'Loading Data of B File
iFile = FreeFile
Open csB For Input As #iFile
Do
Line Input #iFile, sStrComb
sBContent = sBContent & vbCrLf & sStrComb
Loop Until EOF(iFile)
Close #iFile
bCheck = False
'Write combined content
Open csA For Output Access Write As #iFile
Print #iFile, sBefore & sBContent & sAfter
Close #iFile
End Sub
I tried to change it in the following way, but I bump into the run time 1004 error:
Public Sub CombineTextFiles2()
Dim csA As Integer
Dim csB As Integer
Dim bCheck As Boolean
Dim iFile As Integer
Dim sBContent As String, sStrComb As String, sBefore As String, sAfter As String
csA = Worksheets("Sheet1").Cells(x, 5).Value 'A File Path
csB = Worksheets("Sheet1").Cells(x, 4).Value 'B File Path
For x = 9 To 25
'Loading Data of B File
iFile = FreeFile
Open csB For Input As #iFile
Do
Line Input #iFile, sStrComb
sBContent = sBContent & vbCrLf & sStrComb
Loop Until EOF(iFile)
Close #iFile
bCheck = False
'Write combined content
Open csA For Output Access Write As #iFile
Print #iFile, sBefore & sBContent & sAfter
Close #iFile
Next
End Sub
Does somebody has any suggestion how to adjust the code so that it runs smoothly? Thanks in advance
Bookmarks