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:

Change Type (Action)Adjustment TypeAdjustment CodeTransaction TypeTransaction Usage CodeTransaction Type CodeTransaction Number
ALMANUALRESTATE-TCV-ACVMANUALRB2160036703
ALMANUALRESTATE-TCV-ACVMANUALRB2160028613
ALMANUALRESTATE-TCV-ACVMANUALRB2160017970
ALMANUALRESTATE-TCV-ACVMANUALRB2160018550
ALMANUALRESTATE-TCV-ACVMANUALRB2160029053

<colgroup><col span="6"><col></colgroup><tbody>
</tbody>

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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

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