Program Macro to create new workbooks only if more than 50 rows.

rplohocky

Active Member
Joined
Sep 25, 2005
Messages
276
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have to do a data base query that has over 500 rows of data. I need to break up this data into separate workbooks that has a maximum of 50 rows per workbook. Because the query has a different row count each time I need to create a macro that will look down column A and see that their are more than 50 rows and extract the next 50 rows into a new workbook and then look again after that 50 and extract another 50 and so on until the original workbook remains with the first 50 rows of data.

Example: The macro would look at A51:A101 and generate a new workbook and then go back and look at A102:A152 and generate another workbook and so on. This would continue until all rows are clear except for the first 50 rows.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

MattH1

Board Regular
Joined
Jul 15, 2016
Messages
174
I didn't test the code, let me know if this runs as you would like. Also take note that cells 1-50 are in one sheet, 51-100 are in the next (not 51-101 and 102-152).

Code:
Sub Splicer()
Dim NumSheets as Integer
Dim RowCount As Long
Dim CopyCells1 As Integer
Dim CopyCells2 As Integer
RowCount = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

NumSheets = Round((RowCount/5),0) + 1

For I = 1 to NumSheets
Set NewBook = Workbooks.Add
CopyCells1 = 50* (I-1) + 1
CopyCells2 = 50 * (I)
Workbooks("MAINBOOKNAME").Worksheets("MAINWORKSHEETNAME").Cells(Cells(1,CopyCells1),Cells(1,CopyCells2)).EntireRow.Copy 'Edit this with current workbook name and worksheet name
NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
NewBook.SaveAs FileName:= "WHATEVER YOU WANT" & .xlsx 'Edit this part to include the file you want to save it as.
Next I

End Sub
 
Last edited:
Upvote 0

MattH1

Board Regular
Joined
Jul 15, 2016
Messages
174
Round will also round up, I've changed the code below to fix this error:
Code:
Sub Splicer()
Dim NumSheets as Integer
Dim RowCount As Long
Dim CopyCells1 As Integer
Dim CopyCells2 As Integer
RowCount = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

NumSheets = Int((RowCount/5),0) + 1

For I = 1 to NumSheets
Set NewBook = Workbooks.Add
CopyCells1 = 50* (I-1) + 1
CopyCells2 = 50 * (I)
Workbooks("MAINBOOKNAME").Worksheets("MAINWORKSHEETNAME").Cells(Cells(1,CopyCells1),Cells(1,CopyCells2)).EntireRow.Copy 'Edit this with current workbook name and worksheet name
NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
NewBook.SaveAs FileName:= "WHATEVER YOU WANT" & .xlsx 'Edit this part to include the file you want to save it as.
Next I

End Sub
 
Upvote 0

rplohocky

Active Member
Joined
Sep 25, 2005
Messages
276
Office Version
  1. 365
Platform
  1. Windows
MattH1,
I inserted the code and modified the workbook name and sheet name but i'm getting an error in this line:

Workbooks("Book2").Worksheets("DataSheet").Cells(Cells(1, CopyCells1), Cells(1, CopyCells2)).EntireRow.Copy

The names are correct and i can't see anything wrong. However it is creating new workbooks but gets hung up at this line.

Your new code seems to have an issue on this line:

NumSheets = Int((RowCount/5),0) + 1

Not sure what's happening here either. Thanks for your help so far!!
 
Upvote 0

MattH1

Board Regular
Joined
Jul 15, 2016
Messages
174
MattH1,
I inserted the code and modified the workbook name and sheet name but i'm getting an error in this line:

Workbooks("Book2").Worksheets("DataSheet").Cells(Cells(1, CopyCells1), Cells(1, CopyCells2)).EntireRow.Copy

The names are correct and i can't see anything wrong. However it is creating new workbooks but gets hung up at this line.

Your new code seems to have an issue on this line:

NumSheets = Int((RowCount/5),0) + 1

Not sure what's happening here either. Thanks for your help so far!!


Hey rplohocky,
Hope these edits help! Fixed NumSheets and fixed the save file. The problem was that the save file and the piece .xlsx should have been in quotes (my mistake, happens when you don't run the code yourself!!)

I hope this new code gives you no errors, it's just quotes around the .xlsx and a fixed NumSheets formula.

Code:
Sub Splicer()
Dim NumSheets as Integer
Dim RowCount As Long
Dim CopyCells1 As Integer
Dim CopyCells2 As Integer
RowCount = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

NumSheets = RowCount/50 + 1 'This is the edit. It'll give you how many sheets you're making

For I = 1 to NumSheets
Set NewBook = Workbooks.Add
CopyCells1 = 50* (I-1) + 1
CopyCells2 = 50 * (I)
Workbooks("MAINBOOKNAME").Worksheets("MAINWORKSHEETNAME").Cells(Cells(1,CopyCells1),Cells(1,CopyCells2)).EntireRow.Copy 'Edit this with current workbook name and worksheet name
NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
NewBook.SaveAs FileName:= "WHATEVER YOU WANT" & ."xlsx" 'Edit this part to include the file you want to save it as.
Next I

End Sub
 
Upvote 0

Forum statistics

Threads
1,190,610
Messages
5,981,916
Members
439,743
Latest member
KatieO

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
Top