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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi,

A fairly simple macro can filter the master worksheet and create a distinct workbook for each result.
 
Upvote 0
HI
What about
VBA Code:
Sub test()
Dim a, x
Dim i&, n&, c&
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:=ThisWorkbook.Path & "\" & i & ".xlsx"
                ActiveWorkbook.Close SaveChanges:=True
End With
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
HI
What about
VBA Code:
Sub test()
Dim a, x
Dim i&, n&, c&
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:=ThisWorkbook.Path & "\" & i & ".xlsx"
                ActiveWorkbook.Close SaveChanges:=True
End With
Next
Application.ScreenUpdating = True
End Sub
Can you plz add a code that each workbook having the header from master sheet as well
 
Upvote 0
HI
What about
VBA Code:
Sub test()
Dim a, x
Dim i&, n&, c&
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:=ThisWorkbook.Path & "\" & i & ".xlsx"
                ActiveWorkbook.Close SaveChanges:=True
End With
Next
Application.ScreenUpdating = True
End Sub
And also add a code when it splits into multiple then name should be test_1,test_2 like that
 
Upvote 0
And also add a code when it splits into multiple then name should be test_1,test_2 like that
VBA Code:
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\test_" & i & ".xlsx"
 
Upvote 0
Ah sorry
Try
VBA Code:
Sub test()
Dim a, x, h
Dim i&, n&, c&
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(, UBound(a, 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:=ThisWorkbook.Path & "\Test_" & i & ".xlsx"
                ActiveWorkbook.Close SaveChanges:=True
End With
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Ah sorry
Try
VBA Code:
Sub test()
Dim a, x, h
Dim i&, n&, c&
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(, UBound(a, 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:=ThisWorkbook.Path & "\Test_" & i & ".xlsx"
                ActiveWorkbook.Close SaveChanges:=True
End With
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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