Splitting a large Worksheet into multiple work books after n rows while keeping the header row

Zaher503

New Member
Joined
Oct 11, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a large dataset of only 3 columns. I want this set to be split every x rows into a separate excel file, or workbook. I also want the header row to remain the same in all files.
If you want a sample of what I am looking for I can provide some screenshots.

I have looked everywhere without a solution so any help would be great.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Many samples whatever the Excel forum; for more help better an attachment rather than screenshots …​
 
Upvote 0
Hi Zaher,

I think this link can help you and I also copied macro for your reference.




Sub textStreamToExcel()

'Add Scripting references in Tools before you write this code:
'Microsoft Scripting Control 1.0 and Microsoft Scripting Runtime

Dim numOfLines As Long
numOfLines = 10 '################### change this number to suit your needs

'Enter the source file name
Dim vFileName
vFileName = Application.GetOpenFilename("Text Files (*.txt),*.txt")

If vFileName = False Then
Exit Sub
End If

Dim fso As FileSystemObject
Set fso = New FileSystemObject

Dim ts As TextStream
Dim line As String
Dim counter As Long

Set ts = fso.OpenTextFile(vFileName, ForReading)

Dim wkb As Workbook
Set wkb = Workbooks.Add
wkb.Activate
'Save your file, enter your file name if you wish
Dim vSavedFile
vSavedFile = wkb.Application.GetSaveAsFilename(FileFilter:="Excel Files (*.xls), *.xls")


If vSavedFile = False Then
Exit Sub
End If

wkb.SaveAs vSavedFile

Dim cwks As Integer
cwks = wkb.Sheets.Count

Dim iwks As Integer
iwks = 1
Dim wkbS As Excel.Worksheet

Application.ScreenUpdating = False
Looping:
counter = 1
If iwks <= cwks Then
Set wkbS = wkb.Worksheets(iwks)
wkbS.Activate
Range("A1").Activate

While counter <= numOfLines

If ts.AtEndOfStream <> True Then

line = ts.ReadLine
If ActiveCell.Value = "" Then
ActiveCell.Value = CStr(line)
End If
ActiveCell.Offset(1, 0).Activate
counter = counter + 1
Else
ts.Close
GoTo Ending
End If
Wend
Else
Set wkbS = wkb.Worksheets.Add(After:=Sheets(Sheets.Count))
wkbS.Activate
Range("A1").Activate

While counter <= numOfLines

If ts.AtEndOfStream <> True Then

'If the last line has been read it will give you an Input error
line = ts.ReadLine
If ActiveCell.Value = "" Then
ActiveCell.Value = CStr(line)
End If
ActiveCell.Offset(1, 0).Activate
counter = counter + 1
Else
ts.Close
GoTo Ending
End If
Wend
End If

iwks = iwks + 1

If ts.AtEndOfStream <> True Then
GoTo Looping
Else
GoTo Ending
End If

Ending:
Application.ScreenUpdating = True
Set fso = Nothing
Set ts = Nothing
Set wkb = Nothing
Set wkbS = Nothing
MsgBox "Transfer has been completed"
Exit Sub

ErrorHandler:

MsgBox "The following error has occured:" & Chr(13) & Chr(13) & "Error No: " & Err.Number * Chr(13) & "Description: " & Chr(13) & Err.Description

End Sub
 
Upvote 0
What a cumbersome code and does not match the need ‼​
 
Upvote 0
Many samples whatever the Excel forum; for more help better an attachment rather than screenshots …​
Sample.csv
ABC
1Last NameFirst NameEmail Address
2Last Name 1First Name 1example1@gmail.com
3Last Name 2First name 2example2@gmail.com
4Last Name 3First Name 3example3@gmail.com
5Last Name 4First Name 4example1@gmail.com
6Last Name 5First Name 5example2@gmail.com
7Last Name 6First Name 6example3@gmail.com
8Last Name 7First Name 7example1@gmail.com
9Last Name 8First Name 8example2@gmail.com
10Last Name 9First Name 9example3@gmail.com
11Last Name 10First Name 10example1@gmail.com
12Last Name 11First Name 11example2@gmail.com
13Last Name 12First Name 12example3@gmail.com
14Last Name 13First Name 13example1@gmail.com
15Last Name 14First Name 14example2@gmail.com
16Last Name 15First Name 15example3@gmail.com
17Last Name 16First Name 16example1@gmail.com
18Last Name 17First Name 17example2@gmail.com
19Last Name 18First Name 18example3@gmail.com
20Last Name 19First Name 19example1@gmail.com
21Last Name 20First Name 20example2@gmail.com
22Last Name 21First Name 21example3@gmail.com
23Last Name 22First Name 22example1@gmail.com
24Last Name 23First Name 23example2@gmail.com
25Last Name 24First Name 24example3@gmail.com
26Last Name 25First Name 25example1@gmail.com
27Last Name 26First Name 26example2@gmail.com
28Last Name 27First Name 27example3@gmail.com
29Last Name 28First Name 28example1@gmail.com
30Last Name 29First Name 29example2@gmail.com
31Last Name 30First Name 30example3@gmail.com
32Last Name 31First Name 31example1@gmail.com
33Last Name 32First Name 32example2@gmail.com
34Last Name 33First Name 33example3@gmail.com
35Last Name 34First Name 34example1@gmail.com
36Last Name 35First Name 35example2@gmail.com
Sample


I am not very familiar with this tool so I did not know I can do this. But here is a sample of my file. It goes down to 5000 rows and I want to keep the Header row in all new files.
 
Upvote 0
I want this set to be split every x rows into a separate excel file, or workbook.
So what means 'x' ? And which kind of files to create ? And Where ? And … As Accuracy leads to a quicker and more targeted solution …​
 
Upvote 0
As your source file is not an Excel workbook but just a text file so for a faster procedure the better is you link it on a files host website like Dropbox …​
As it's a text file did you test the post #3 code ?​
 
Upvote 0
As your source file is not an Excel workbook but just a text file so for a faster procedure the better is you link it on a files host website like Dropbox …​
As it's a text file did you test the post #3 code ?​
I tried post #3 but it did not work. I got a compiler error: User-defined type not defined
I have uploaded a sample of the file I am working with if you want to take a look.
Dropbox
 
Upvote 0

Ok for the sample text file but any answer to my post #6 ?!​
 
Upvote 0
So what means 'x' ? And which kind of files to create ? And Where ? And … As Accuracy leads to a quicker and more targeted solution …​
Lol you are right. My post was very vague, I am sorry.
1- Let's say every 100 rows. But is there a way to customize this in case I want more rows in the future?
2- Creating an xlsx file would be great.
3- In a folder called output in the same directory as the origin file.
4- file name for output can be sequential numbers starting at 1.xlsx

If there is any more information you need just let me know!
 
Upvote 0

Forum statistics

Threads
1,223,632
Messages
6,173,472
Members
452,516
Latest member
archcalx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top