Hi everyone, the following Macro works really well. You select the text file you want to split into smaller files, you then tell it how many rows max per new file you want, 10,000 for example. I would like for the macro to also keep the headings (first row) at the start of every new file created. Any help would be appreciated.
Public Sub Split_Text_File()
Dim FSO As Object
Dim TSRead As Object, TSWrite As Object
Dim inputFile As Variant, outputFile As String
Dim part As Long, i As Long, n As Long, p As Long
Dim maxRows As Long
inputFile = Application.GetOpenFilename(Title:="Select a text file to be split into separate parts")
If inputFile = False Then Exit Sub
maxRows = Application.InputBox("Max number of lines/rows?", Type:=1)
If maxRows = 0 Then Exit Sub
ReDim outputLines(maxRows - 1) As String
p = InStrRev(inputFile, ".")
part = 0
n = 0
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TSRead = FSO.OpenTextFile(inputFile)
While Not TSRead.AtEndOfStream
outputLines(n) = TSRead.ReadLine
n = n + 1
If n = maxRows Then
part = part + 1
outputFile = Left(inputFile, p - 1) & " PART" & part & Mid(inputFile, p)
Set TSWrite = FSO.CreateTextFile(outputFile, True)
TSWrite.Write Join(outputLines, vbCrLf)
TSWrite.Close
ReDim outputLines(maxRows - 1) As String
n = 0
End If
Wend
TSRead.Close
If n > 0 Then
ReDim outputlines2(n - 1) As String
For i = 0 To n - 1
outputlines2(i) = outputLines(i)
Next
part = part + 1
outputFile = Left(inputFile, p - 1) & " PART" & part & Mid(inputFile, p)
Set TSWrite = FSO.CreateTextFile(outputFile, True)
TSWrite.Write Join(outputlines2, vbCrLf)
TSWrite.Close
End If
MsgBox "Done"
End Sub