Hello,
1. I have 960 txt files each containing a single value.
2. The txt files are named as case001.txt, case002.txt,.....,case960.txt
2. I would like to make 10 excel files each containing 8x12 cells/array out of every 96 files over 960 files.
The following scripts should do the job but with no success.
Many many thanks in advance!
1. I have 960 txt files each containing a single value.
2. The txt files are named as case001.txt, case002.txt,.....,case960.txt
2. I would like to make 10 excel files each containing 8x12 cells/array out of every 96 files over 960 files.
The following scripts should do the job but with no success.
Many many thanks in advance!
Code:
Sub ReadTextFiles()
' Make reference to library:
' Tools -> References -> Microsoft Scripting Runtime
Dim iRow As Integer, iCol As Integer
Dim iBooksCounter As Integer, iTextCounter As Integer
Dim fso As New FileSystemObject
Dim txt As TextStream, aFile As File
Dim sContent As String, wkb As Workbook
For Each aFile In fso.GetFolder(ThisWorkbook.Path).Files
If fso.GetExtensionName(aFile.Name) = "txt" Then
If iTextCounter Mod 96 = 0 Or iTextCounter = 0 Then
iRow = 1: iCol = 1: iBooksCounter = iBooksCounter + 1
If Not wkb Is Nothing Then
With wkb
.SaveAs Filename:=ThisWorkbook.Path & "\ExcelFile" & iBooksCounter & ".xlsx"
.Close SaveChanges:=True
End With
End If
Set wkb = Workbooks.Add
End If
Set txt = fso.OpenTextFile(Filename:=aFile.Path, IOMode:=ForReading)
sContent = txt.ReadAll
With wkb.Sheets(1)
iCol = iCol + 1
If iCol = 13 Then
iRow = iRow + 1: iCol = 1
End If
.Cells(iRow, iCol) = sContent
End With
End If
Next
End Sub