VBA Code to Split Master Data into multiple workbooks by Row Count

KaranM

New Member
Joined
May 29, 2020
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I'm looking for a VBA Code that can help me to split master data into multiple workbooks by row count.

The sheet has about 50k rows and needs to split by 150 Rows into each workbook

Row 1 to 5 is basically the header and needs to be there in every sheet including the formatting & spacing.

Row 6 onwards is where the data starts and needs to be split into batches of 150 rows
For Example
Row 6-155 = 150 Rows split into a new workbook with the header from Row 1 to 5
Row 156-305 = 150 Rows split into a new workbook with the header from Row 1 to 5

and so forth

Any help would be appreciated.

Thank you.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Welcome to the forum
- this creates the separate workbooks
- row numbers are embedded in their names
FileList.jpg



For the first test ...
1. Test on a COPY of your workbbok
2. Place code in a NEW module
3. Amend name of sheet containing master data
Set wS = ThisWorkbook.Sheets("MasterData")
- amend path to save workbooks
Const SavePath = "C:\folder\subfolder"

Amending ...
If you need any help amending the individual workbook names etc let me know

VBA Code:
Option Explicit

Sub SeparateWorkbooks()
    Dim wS As Worksheet, Temp As Worksheet, Hdr As Range, Data As Range, aName As String
    Dim LastRow As Long, LastCol As Long, r As Long, r2 As Long
    Const H = 5, NoRows = 150
    Application.ScreenUpdating = False
    Set wS = ThisWorkbook.Sheets("MasterData")
    LastCol = wS.Cells.Find("*", wS.Cells(1, 1), xlFormulas, xlWhole, xlByColumns, xlPrevious).Column
    LastRow = wS.Cells(Rows.Count, 1).End(xlUp).Row
   
'add temp sheet with headers and formating
    wS.Copy Before:=Sheets(1)
    Set Temp = Sheets(1)
    Set Hdr = Temp.Rows(1).Resize(H, LastCol)
    Set Data = Hdr.Offset(5).Resize(NoRows)
    Data.ClearContents
    Data.Offset(NoRows).Resize(Rows.Count - H - NoRows).EntireRow.Delete
   
'copy the data to temp sheet and save to workbook
    For r = 6 To LastRow Step NoRows
        wS.Rows(r).Resize(NoRows, LastCol).Copy Data
        r2 = WorksheetFunction.Min(LastRow, r + NoRows)
        aName = "Rows " & Format(r, "00000") & " to " & Format(r2, "00000")
        Call SaveToWorkBook(Temp, aName)
    Next r

'delete temporary sheet
    Application.DisplayAlerts = False
    Temp.Delete
    Application.DisplayAlerts = True
End Sub


Private Sub SaveToWorkBook(Temp As Worksheet, aName)
    Const SavePath = "C:\folder\subfolder"
    Dim wb As Workbook
    Set wb = Workbooks.Add
    Temp.Copy Before:=wb.Sheets(1)
    wb.SaveAs SavePath & "\" & aName
    wb.Close False
End Sub
 
Upvote 0
Thank you.

Tried this and it works.

However the split files are to be fed into a platform using mass upload and somehow it says format not valid.

I don't mind sending you the original file to you personally if that would be ok for you to have a look.

Let me know.

Once again thank you.
 
Upvote 0
I will send updated code later today with output to a different formats for you to test
 
Upvote 0
However the split files are to be fed into a platform using mass upload and somehow it says format not valid.

I misunderstood your problem - I thought you meant you needed a different Excel format :unsure:
That is not correct - you need a different layout


I do not understand what you now want
The linked workbook contains no explanations

If you want any further help, please supply the following ...
- a small sample and layout of the ORIGINAL master data
- layout of expected results using the same data
 
Upvote 0
Hi

My bad for the confusion

Please check the following link


I have updated the file with a sample data with 1000 rows (Data starts from Row 6)

The file is the original template file provided by the platform & have filled in the sample data for your reference

The platform has an upload limit of 150 SKU per file, hence the reason for needing each file to only have 150 SKU.
Each workbook needs to be in the same exact format for the platform to accept the file, otherwise, it keeps on saying format not valid.


Thank you
 
Upvote 0
thanks - will look at you workbook with the next 48 hours and update this thread after that
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,303
Members
449,078
Latest member
nonnakkong

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