Separate a master workbook into multiple workbooks based on a range/number of rows

chris1979

Board Regular
Joined
Feb 23, 2016
Messages
52
Hi
Could you please assist

I have a master workbook with over 225,000 rows and I would require to be separated into different workbooks with 6000 rows each and the remainder into the last workbook
Each file should retain column header and all the workbooks should be named as Workbook1, Workbook2 etc

Regards
CT
 

Attachments

  • Example.PNG
    Example.PNG
    14 KB · Views: 4

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Create separate workbooks from a master workbook
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Response from another site

Sub Demo1()
Dim L&, F&, N%
L = 1
With Application
.DisplayAlerts = False
.ScreenUpdating = False
Workbooks.Add xlWBATWorksheet
With [A1].CurrentRegion.Rows
.Item(1).Copy ActiveSheet.[A1]
Do
F = L + 1
L = L + 5999: If L > .Count Then L = .Count
.Item(F & ":" & L).Copy ActiveSheet.[A2]
N = N + 1
ActiveWorkbook.SaveAs Parent.Path & "\Workbook" & Format(N, " 000 "), 51
If L < .Count Then ActiveSheet.UsedRange.Offset(1).Clear Else Exit Do
Loop
End With
ActiveWorkbook.Close
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub
 
Upvote 0
Solution
What I personally did was
Create a separate helper column with - =FLOOR(ROW()/6000,1)
Searched for a code to sort the range based on a column. Came across this code

Function RemoveDuplicates(cities As Range) As Range

ThisWorkbook.Activate
Sheets.Add
On Error Resume Next
ActiveSheet.Name = "Path"
Sheets("cities").Activate
On Error GoTo 0

cities.Copy
Cells(2, 1).Activate
ActiveCell.PasteSpecial xlPasteValues
Range("A1").Value = "Cities"

Dim lstRow As Long
lstRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:A" & lstRow).Select
ActiveSheet.Range(Selection.Address).RemoveDuplicates Columns:=1, Header:=xlNo
lstRow = Cells(Rows.Count, 1).End(xlUp).Row
Set RemoveDuplicates = Range("A2:A" & lstRow)
End Function

Sub creatfiles(cities As Range, clmNo As Long)
Dim wb As Workbook 'for gh files
Dim foldPath As String 'folder path for saving files

Application.FileDialog(msoFileDialogFolderPicker).Show
foldPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)

ThisWorkbook.Activate
For Each cell In cities
Sheet1.Activate
Dim lstClm As Long
Dim lstRow As Long
lstRow = Cells(Rows.Count, 1).End(xlUp).Row
lstClm = Cells(1, Columns.Count).End(xlToLeft).Column

Dim dataSet As Range
Set dataSet = Range(Cells(1, 1), Cells(lstRow, lstClm))
dataSet.AutoFilter field:=clmNo, Criteria1:=cell.Value

lstRow = Cells(Rows.Count, 1).End(xlUp).Row
lstClm = Cells(1, Columns.Count).End(xlToLeft).Column
Debug.Print lstRow; lstClm
Set dataSet = Range(Cells(1, 1), Cells(lstRow, lstClm))

dataSet.Copy
Set wb = Workbooks.Add
ActiveCell.PasteSpecial xlPasteAll

wb.SaveAs foldPath & "/" & cell.Value
wb.Close
Dim fullFileName As Range
Set fullFileName = ThisWorkbook.Sheets("cities").Range(cell.Address).Offset(0, 1)
Debug.Print cell.Address
fullFileName.Value = foldPath & "/" & cell.Value & ".xlsx"
Debug.Print fullFileName.Address
ThisWorkbook.Activate
Debug.Print foldPath & "/" & cell.Value
Next cell
End Sub
Sub Spliter()

With Application
.ScreenUpdating = False
.DisplayAlerts = False
.AlertBeforeOverwriting = False
.Calculation = xlCalculationManual
End With

ThisWorkbook.Activate
Sheet1.Activate

'clearing filer if any
On Error Resume Next
Sheet1.ShowAllData
On Error GoTo 0

Dim lsrClm As Long
Dim lstRow As Long

lstRow = Cells(Rows.Count, 1).End(xlUp).Row
Dim cities As Range
Dim clm As String, clmNo As Long
On Error GoTo handler
clm = Application.InputBox("From which column you want create files" & vbCrLf & "E.g. A,B,C,AB,ZA etc.")
clmNo = Range(clm & "1").Column
Set cities = Range(clm & "2:" & clm & lstRow)
Set cities = RemoveDuplicates(cities)
Call creatfiles(cities, clmNo)


With Application
.ScreenUpdating = True
.DisplayAlerts = True
.AlertBeforeOverwriting = True
.Calculation = xlCalculationAutomatic
End With
Data.ShowAllData
MsgBox "Well Done!"
Exit Sub
Data.ShowAllData

handler:
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.AlertBeforeOverwriting = True
.Calculation = xlCalculationAutomatic
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,816
Members
449,049
Latest member
cybersurfer5000

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