Split an Excel sheet into N number of distinct workbook

Tanu

New Member
Joined
Feb 6, 2023
Messages
21
Office Version
  1. 365
Platform
  1. Windows
How we can write a VBA code for spilt an excel sheet into multiple workbooks if it contains lakhs of records...for eg - we have 10k records in master sheet now we want 2 workbook of 5k-5k chunks then how we can automatically do this
Or if I give 4 as a number for 10k records then it should divide it & validate it and spilts the workbook accordingly
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi
The code in post#7
If it's not what you want please elaborate your needs
 
Upvote 0
Hi
The code in post#7
If it's not what you want please elaborate your nee
Actually if there is 2 header in a master sheet then it should copy those 2 headers in each workbook...
 
Upvote 0
Actually if there is 2 header in a master sheet then it should copy those 2 headers in each workbook...
Can we make a change in this code
Like I'm browsing a file through VBA and I want this code should check that workbook which we have browsed and accordingly it splits
 
Upvote 0
VBA Code:
Sub test()
Dim a, x
Dim i&, n&, c&
Dim Myfile As Variant
Dim p As String
Myfile = Application.GetOpenFilename()
If Myfile = False Then Exit Sub
Workbooks.Open Myfile
p = ActiveWorkbook.path
a = Sheets("sheet1").Cells(1, 1).CurrentRegion
n = InputBox("HOW MANY WORKBOOKS?")
x = Application.RoundUp((UBound(a) / n), 0)
Application.ScreenUpdating = False
For i = 1 To n
        Workbooks.Add
            With ActiveSheet
                .Cells(1, 1).Resize(x, UBound(a, 2)) = Application.IfError(Application.Transpose( _
                            Application.Index(a, Application.Transpose(Evaluate("row(" _
                                            & c + 1 & ":" & c + x & ")")), Evaluate("row(1:" & UBound(a, 2) & ")"))), "")
                            c = c + x
                ActiveWorkbook.SaveAs fileName:=p & "\Test_" & i & ".xlsx"
                ActiveWorkbook.Close SaveChanges:=True
End With
Next
 ActiveWorkbook.Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub
 
Upvote 0
VBA Code:
Sub test()
Dim a, x
Dim i&, n&, c&
Dim Myfile As Variant
Dim p As String
Myfile = Application.GetOpenFilename()
If Myfile = False Then Exit Sub
Workbooks.Open Myfile
p = ActiveWorkbook.path
a = Sheets("sheet1").Cells(1, 1).CurrentRegion
n = InputBox("HOW MANY WORKBOOKS?")
x = Application.RoundUp((UBound(a) / n), 0)
Application.ScreenUpdating = False
For i = 1 To n
        Workbooks.Add
            With ActiveSheet
                .Cells(1, 1).Resize(x, UBound(a, 2)) = Application.IfError(Application.Transpose( _
                            Application.Index(a, Application.Transpose(Evaluate("row(" _
                                            & c + 1 & ":" & c + x & ")")), Evaluate("row(1:" & UBound(a, 2) & ")"))), "")
                            c = c + x
                ActiveWorkbook.SaveAs fileName:=p & "\Test_" & i & ".xlsx"
                ActiveWorkbook.Close SaveChanges:=True
End With
Next
 ActiveWorkbook.Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub
This is perfect thankyou so much
A last query can we make the workbook name as dynamic instead of test it reads current workbook name and update the splits workbook accordingly
 
Upvote 0
Hi
What about
VBA Code:
Sub test()
Dim a, x, h
Dim i&, n&, c&
Dim Myfile As Variant
Dim p, nm As String
Myfile = Application.GetOpenFilename()
If Myfile = False Then Exit Sub
Workbooks.Open Myfile
p = ActiveWorkbook.path
nm = Split(ActiveWorkbook.Name, ".")(0)
a = Sheets("sheet1").Cells(1, 1).CurrentRegion
h = Sheets("sheet1").Cells(1, 1).CurrentRegion.Resize(1)
n = InputBox("HOW MANY WORKBOOKS?")
x = Application.RoundUp((UBound(a) / n), 0)
c = 1
Application.ScreenUpdating = False
For i = 1 To n
        Workbooks.Add
            With ActiveSheet
                .Cells(1, 1).Resize(, 2) = h
                .Cells(2, 1).Resize(x, UBound(a, 2)) = Application.IfError(Application.Transpose( _
                            Application.Index(a, Application.Transpose(Evaluate("row(" _
                                            & c + 1 & ":" & c + x & ")")), Evaluate("row(1:" & UBound(a, 2) & ")"))), "")
                            c = c + x
                ActiveWorkbook.SaveAs fileName:=p & "\" & nm & i & ".xlsx"
                ActiveWorkbook.Close SaveChanges:=True
End With
Next
 ActiveWorkbook.Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub
 
Upvote 0
@ mohadin

All my Congratulations ... for your Immeasurable Patience (y)
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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