Split rows EQUALLY into multiple sheets

Alberto15

New Member
Joined
Jun 30, 2021
Messages
7
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
  2. MacOS
  3. Web
Hello all,
Trust you're good. I got this script from Kutools that allow me to split data into sheets. For example I need to split 1 sheet of 5000 rows into 5 sheets of 1000 rows. My issue is that when splitting the sheet, it decrease the rows by 1000 only (sheet 1 5000 rows, sheet 2 4000 rows, sheet 3 3000 rows). Instead of copying I tried to cut but I got blank excel then i tried to delete the exceeding rows and i got errors.

Grateful if you could help.


VBA Code:
'split data into Sheets
Sub SplitDataIntoSheets()
Dim WorkRng As Range
Dim NumRow As Range
Dim SplitRow As Integer
Dim ws As Worksheet
On Error Resume Next

TitleID = "Distribution"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Press Ctrl A then modify $A$1 to $A$2", TitleID, WorkRng.Address, Type:=8)
SplitRow = Application.InputBox("Number of rows", TitleID, 800, Type:=1)
Set ws = WorkRng.Parent
Set NumRow = WorkRng.Rows(1)

Application.ScreenUpdating = False

For i = 1 To WorkRng.Rows.Count Step SplitRow
    resizeCount = SplitRow
    If (WorkRng.Rows.Count - xRow.Row + 1) < SplitRow Then resizeCount = WorkRng.Rows.Count - NumRow.Row + 1
    NumRow.Resize(resizeCount).Copy
    Application.Worksheets.Add after:=Application.Worksheets(Application.Worksheets.Count)
    Application.ActiveSheet.Range("A2").PasteSpecial
    Set NumRow = NumRow.Offset(SplitRow)
Next

Application.CutCopyMode = False
Application.ScreenUpdating = False
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I need to split 1 sheet of 5000 rows into 5 sheets of 1000 rows.

Try the following, adjust n = 1000 for the desired number of lines.
Assuming your data starts in cell A2.
Run the macro on the sheet that you want to make the separation.


VBA Code:
Sub splitData()
  Dim sh As Worksheet
  Dim i As Long, n As Long
  
  n = 1000
  Set sh = ActiveSheet
  For i = 2 To Range("A" & Rows.Count).End(3).Row Step n
    Sheets.Add , Sheets(Sheets.Count)
    sh.Range("A" & i).Resize(n).EntireRow.Copy Range("A2")
  Next
End Sub
 
Upvote 0
Try the following, adjust n = 1000 for the desired number of lines.
Assuming your data starts in cell A2.
Run the macro on the sheet that you want to make the separation.


VBA Code:
Sub splitData()
  Dim sh As Worksheet
  Dim i As Long, n As Long
 
  n = 1000
  Set sh = ActiveSheet
  For i = 2 To Range("A" & Rows.Count).End(3).Row Step n
    Sheets.Add , Sheets(Sheets.Count)
    sh.Range("A" & i).Resize(n).EntireRow.Copy Range("A2")
  Next
End Sub
Hello @DanteAmor ,Thanks for your response. Yes my cell will starts at A2 but the desired number will be dynamic as it will need to be adjusted depending on the situation.
 
Upvote 0
the desired number will be dynamic

You mean this:
VBA Code:
Sub splitData()
  Dim sh As Worksheet
  Dim i As Long, n As Long
 
  n = Application.InputBox("Number of rows", "Distribution", 1000, Type:=1)
  If n = 0 Then Exit Sub
  Set sh = ActiveSheet
  For i = 2 To sh.Range("A" & Rows.Count).End(3).Row Step n
    Sheets.Add , Sheets(Sheets.Count)
    sh.Range("A" & i).Resize(n).EntireRow.Copy Range("A2")
  Next
End Sub
 
Upvote 0
You mean this:
VBA Code:
Sub splitData()
  Dim sh As Worksheet
  Dim i As Long, n As Long
 
  n = Application.InputBox("Number of rows", "Distribution", 1000, Type:=1)
  If n = 0 Then Exit Sub
  Set sh = ActiveSheet
  For i = 2 To sh.Range("A" & Rows.Count).End(3).Row Step n
    Sheets.Add , Sheets(Sheets.Count)
    sh.Range("A" & i).Resize(n).EntireRow.Copy Range("A2")
  Next
End Sub

when executing, I got empty sheets
 
Upvote 0
Check the data on your sheet, in column A, maybe you have blank spaces inside the cells.
Or do you have formulas in column A?
 
Upvote 0
when executing, I got empty sheets
I tried the script and it worked for me.

I suspect you had some empty sheets to begin with.
The script adds new sheets and puts your scripts data in the new sheets.
 
Upvote 0
I still got empty sheets and I don't have empty sheets nor empty spaces.. I tried on other sheets but still the same, on both .csv and .xlxs. here is the minisheet...

test1.xlsx
ABCDEF
1Name1Test1office1Address1Address2Address3
2Name22Test22office22Address22Address23Address24
3Name23Test23office23Address23Address24Address25
4Name24Test24office24Address24Address25Address26
5Name25Test25office25Address25Address26Address27
6Name26Test26office26Address26Address27Address28
7Name27Test27office27Address27Address28Address29
8Name28Test28office28Address28Address29Address30
9Name29Test29office29Address29Address30Address31
10Name30Test30office30Address30Address31Address32
11Name31Test31office31Address31Address32Address33
12Name32Test32office32Address32Address33Address34
13Name33Test33office33Address33Address34Address35
14Name34Test34office34Address34Address35Address36
15Name35Test35office35Address35Address36Address37
16Name36Test36office36Address36Address37Address38
17Name37Test37office37Address37Address38Address39
18Name38Test38office38Address38Address39Address40
19Name39Test39office39Address39Address40Address41
20Name40Test40office40Address40Address41Address42
21Name41Test41office41Address41Address42Address43
22Name22Test22office22Address22Address23Address24
23Name23Test23office23Address23Address24Address25
24Name24Test24office24Address24Address25Address26
25Name25Test25office25Address25Address26Address27
26Name26Test26office26Address26Address27Address28
27Name27Test27office27Address27Address28Address29
28Name28Test28office28Address28Address29Address30
29Name29Test29office29Address29Address30Address31
30Name30Test30office30Address30Address31Address32
31Name31Test31office31Address31Address32Address33
32Name32Test32office32Address32Address33Address34
33Name33Test33office33Address33Address34Address35
34Name34Test34office34Address34Address35Address36
35Name35Test35office35Address35Address36Address37
36Name36Test36office36Address36Address37Address38
37Name37Test37office37Address37Address38Address39
38Name38Test38office38Address38Address39Address40
39Name39Test39office39Address39Address40Address41
40Name40Test40office40Address40Address41Address42
41Name41Test41office41Address41Address42Address43
Sheet1
 
Upvote 0
In your example, there are only 41 records. If you go to cell A41 and press the End key and then the down arrow, where does the cursor stop.
 
Upvote 0
In your example, there are only 41 records. If you go to cell A41 and press the End key and then the down arrow, where does the cursor stop.
It stops at 1001, then I copied to 41 rows to test on a new sheets as
VBA Code:
n = Application.InputBox("Number of rows", "Distribution", 1000, Type:=1)
can be modified and easy to work with.
I keep on trying and I'm stuck.
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,973
Members
449,059
Latest member
oculus

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