how to split data into multiple workbook with hearder

Learning1

New Member
Joined
Aug 13, 2018
Messages
15
Hi All,

I need some help with how to transfer data into multiple workbook with header . I have macro which split the data but don't bring the header into workbook 2 and workbook3 and etc and I like to do that please. Below data is little sample but I have 100K of rows and bigger header name.. I just want to bring 8000 rows to each new workbook with same header.. as you can see th macro does that but not able to bring header along with it. thanks for your help!

Example below of the data:

[TABLE="width: 549"]
<colgroup><col span="6"><col></colgroup><tbody>[TR]
[TD]Change Type (Action)[/TD]
[TD]Adjustment Type[/TD]
[TD]Adjustment Code[/TD]
[TD]Transaction Type[/TD]
[TD]Transaction Usage Code[/TD]
[TD]Transaction Type Code[/TD]
[TD]Transaction Number[/TD]
[/TR]
[TR]
[TD]AL[/TD]
[TD]MANUAL[/TD]
[TD]RESTATE-TCV-ACV[/TD]
[TD]MANUAL[/TD]
[TD]R[/TD]
[TD]B[/TD]
[TD="align: right"]2160036703[/TD]
[/TR]
[TR]
[TD]AL[/TD]
[TD]MANUAL[/TD]
[TD]RESTATE-TCV-ACV[/TD]
[TD]MANUAL[/TD]
[TD]R[/TD]
[TD]B[/TD]
[TD="align: right"]2160028613[/TD]
[/TR]
[TR]
[TD]AL[/TD]
[TD]MANUAL[/TD]
[TD]RESTATE-TCV-ACV[/TD]
[TD]MANUAL[/TD]
[TD]R[/TD]
[TD]B[/TD]
[TD="align: right"]2160017970[/TD]
[/TR]
[TR]
[TD]AL[/TD]
[TD]MANUAL[/TD]
[TD]RESTATE-TCV-ACV[/TD]
[TD]MANUAL[/TD]
[TD]R[/TD]
[TD]B[/TD]
[TD="align: right"]2160018550[/TD]
[/TR]
[TR]
[TD]AL[/TD]
[TD]MANUAL[/TD]
[TD]RESTATE-TCV-ACV[/TD]
[TD]MANUAL[/TD]
[TD]R[/TD]
[TD]B[/TD]
[TD="align: right"]2160029053[/TD]
[/TR]
</tbody>[/TABLE]

macro code:

Sub breakfiles()
Dim wb As Workbook
Dim ThisSheet As Worksheet
Dim NumOfColumns As Integer
Dim RangeToCopy As Range
Dim WorkbookCounter As Integer
Dim RowsInFile
Dim Prefix As String
'Const header_Row = 1

Application.ScreenUpdating = False

'Initialize data
Set ThisSheet = ThisWorkbook.ActiveSheet
NumOfColumns = ThisSheet.UsedRange.Columns.Count
WorkbookCounter = 1
RowsInFile = 8000 'how many rows (incl. header) in new files?
Prefix = "test" 'prefix of the file name

For p = 1 To ThisSheet.UsedRange.Rows.Count Step RowsInFile
Set wb = Workbooks.Add

'Paste the chunk of rows for this file

Set RangeToCopy = ThisSheet.Range(ThisSheet.Cells(p, 1), ThisSheet.Cells(p + RowsInFile - 1, NumOfColumns))
RangeToCopy.Copy wb.Sheets(1).Range("A1")

'Save the new workbook, and close it
wb.SaveAs ThisWorkbook.Path & "" & Prefix & "_" & WorkbookCounter
wb.Close

'Increment file counter
WorkbookCounter = WorkbookCounter + 1
Next p

Application.ScreenUpdating = True
Set wb = Nothing
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi Learning1

Welcome to the MrExcel board.

I have not stepped through your code or really looked hard at it. That said, what would happen if you changed this line

Code:
Set RangeToCopy = ThisSheet.Range(ThisSheet.Cells([COLOR=#ff0000]p[/COLOR], 1), ThisSheet.Cells([COLOR=#ff0000]p[/COLOR] + RowsInFile - 1, NumOfColumns))

to this

Code:
Set RangeToCopy = ThisSheet.Range(ThisSheet.Cells([COLOR=#ff0000]1[/COLOR], 1), ThisSheet.Cells([COLOR=#ff0000]1[/COLOR] + RowsInFile - 1, NumOfColumns))
 
Upvote 0
Hi Learning1,

Welcome to MrExcel.com!

I preferred to rewrite my version, so it might look different than yours. You can adapt yours accordingly if you like, this is just to give the idea.
It simply copies the first row into the new workbook first, then copies the necessary range defined with the row count by using limit variable.

Code:
Sub doSplitRows()
Dim sht As Worksheet
Dim head As Range
Dim rng As Range
Dim limit As Integer
Dim cll As Range
Dim wrk As Workbook
Dim prefix As String
Dim i As Integer


    Application.ScreenUpdating = False


    'Workbook filename prefix
    prefix = "test"


    'Number of rows to create a new workbook
    limit = 8000


    Set sht = ActiveSheet
    Set head = sht.Rows(1)
    Set cll = sht.Cells(2, 1)
    Do Until cll.Value = ""
        i = i + 1
        Set rng = cll.Resize(limit, cll.End(xlToRight).Column)
        Set wrk = Application.Workbooks.Add
        head.Copy wrk.Worksheets(1).Cells(1, 1)
        rng.Copy wrk.Worksheets(1).Cells(2, 1)
        wrk.SaveAs ThisWorkbook.Path & Application.PathSeparator & prefix & "_" & Format(i, "0000") 'Trying to make filename zero padded
        wrk.Close
        Set cll = cll.Offset(limit)
    Loop
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Hi Learning1,

Welcome to MrExcel.com!

I preferred to rewrite my version, so it might look different than yours. You can adapt yours accordingly if you like, this is just to give the idea.
It simply copies the first row into the new workbook first, then copies the necessary range defined with the row count by using limit variable.

Code:
Sub doSplitRows()
Dim sht As Worksheet
Dim head As Range
Dim rng As Range
Dim limit As Integer
Dim cll As Range
Dim wrk As Workbook
Dim prefix As String
Dim i As Integer


    Application.ScreenUpdating = False


    'Workbook filename prefix
    prefix = "test"


    'Number of rows to create a new workbook
    limit = 8000


    Set sht = ActiveSheet
    Set head = sht.Rows(1)
    Set cll = sht.Cells(2, 1)
    Do Until cll.Value = ""
        i = i + 1
        Set rng = cll.Resize(limit, cll.End(xlToRight).Column)
        Set wrk = Application.Workbooks.Add
        head.Copy wrk.Worksheets(1).Cells(1, 1)
        rng.Copy wrk.Worksheets(1).Cells(2, 1)
        wrk.SaveAs ThisWorkbook.Path & Application.PathSeparator & prefix & "_" & Format(i, "0000") 'Trying to make filename zero padded
        wrk.Close
        Set cll = cll.Offset(limit)
    Loop
    
    Application.ScreenUpdating = True
    
End Sub



Thank so much that worked awesomely!! thank you!
 
Upvote 0
sorry it was copying the same data over and over..however, it did bring header into files.. thank you for your response and help!
 
Upvote 0
Sorry it was copying the same data over and over but did bring header into other books.. Thanks for your response and help!
 
Upvote 0
Yeah it was just a quick glance. Afterwards I went back and tested it and saw the same thing. I am glad that smozgur was able to provide you with something that works.

Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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