How to split the set of data into specify number of rows on multiple sheets

noppph

New Member
Joined
Feb 22, 2016
Messages
47
I'm having a issue to split my data set into multiple sheets with specify number of rows with their headers from input sheet.


I want to use Excel-VBA to solve this, Thank you...


Example: My input dataset
[TABLE="width: 128"]
<tbody>[TR]
[TD="class: xl65, width: 64"]Name[/TD]
[TD="class: xl65, width: 64"]Cost[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12000[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12001[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12002[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12003[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12004[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12005[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12006[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12007[/TD]
[/TR]
[TR]
[TD="class: xl65"]Aun[/TD]
[TD="class: xl65, align: right"]987000[/TD]
[/TR]
[TR]
[TD="class: xl65"]Aun[/TD]
[TD="class: xl65, align: right"]987001[/TD]
[/TR]
[TR]
[TD="class: xl65"]Aun[/TD]
[TD="class: xl65, align: right"]987002[/TD]
[/TR]
[TR]
[TD="class: xl65"]Aun[/TD]
[TD="class: xl65, align: right"]987003[/TD]
[/TR]
[TR]
[TD="class: xl65"]Aun[/TD]
[TD="class: xl65, align: right"]987004[/TD]
[/TR]
[TR]
[TD="class: xl65"]Aun[/TD]
[TD="class: xl65, align: right"]987005[/TD]
[/TR]
[TR]
[TD="class: xl65"]Aun[/TD]
[TD="class: xl65, align: right"]987006[/TD]
[/TR]
[TR]
[TD="class: xl65"]Aun[/TD]
[TD="class: xl65, align: right"]987007[/TD]
[/TR]
[TR]
[TD="class: xl65"]Aun[/TD]
[TD="class: xl65, align: right"]987008[/TD]
[/TR]
[TR]
[TD="class: xl65"]Fon[/TD]
[TD="class: xl65, align: right"]98752.25[/TD]
[/TR]
[TR]
[TD="class: xl65"]Fon[/TD]
[TD="class: xl65, align: right"]98753.25[/TD]
[/TR]
[TR]
[TD="class: xl65"]Fon[/TD]
[TD="class: xl65, align: right"]98754.25[/TD]
[/TR]
[TR]
[TD="class: xl65"]Fon[/TD]
[TD="class: xl65, align: right"]98755.25[/TD]
[/TR]
[TR]
[TD="class: xl65"]Fon[/TD]
[TD="class: xl65, align: right"]98756.25[/TD]
[/TR]
[TR]
[TD="class: xl65"]Fon[/TD]
[TD="class: xl65, align: right"]98757.25[/TD]
[/TR]
[TR]
[TD="class: xl65"]Fon[/TD]
[TD="class: xl65, align: right"]98758.25[/TD]
[/TR]
[TR]
[TD="class: xl65"]Fon[/TD]
[TD="class: xl65, align: right"]98759.25[/TD]
[/TR]
</tbody>[/TABLE]


These are the output table that i'm looking for.
Sheet1, Sheet2, etc are the output sheets that i want to get. **Each of my sheets can contain only 6 rows(header included).

Sheet1:
[TABLE="width: 128"]
<tbody>[TR]
[TD="class: xl65, width: 64"]Name[/TD]
[TD="class: xl65, width: 64"]Cost[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12000[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12001[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12002[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12003[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12004[/TD]
[/TR]
</tbody>[/TABLE]

Sheet2:
[TABLE="width: 128"]
<tbody>[TR]
[TD="class: xl65, width: 64"]Name[/TD]
[TD="class: xl65, width: 64"]Cost[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12005[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12006[/TD]
[/TR]
[TR]
[TD="class: xl65"]Dew[/TD]
[TD="class: xl65, align: right"]12007[/TD]
[/TR]
[TR]
[TD="class: xl65"]Aun[/TD]
[TD="class: xl65, align: right"]987000[/TD]
[/TR]
[TR]
[TD="class: xl65"]Aun[/TD]
[TD="class: xl65, align: right"]987001[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi nopph,

use below macro:-

Code:
Sub datasplit()
'created by DILIPandey on 23-Feb'16
Range("a2").Select
i = 2
n = 0
s = ActiveSheet.Name
While ActiveCell.Value <> ""
    Range(Range("a" & i * 4 - 6 + n), Range("a" & i * 4 - 6 + n).Offset(4, 1)).Copy
    Sheets.Add
    Range("a2").PasteSpecial
    Range("a1").Value = "Name"
    Range("b1").Value = "Cost"
        i = i + 1
        n = n + 1
    Sheets(s).Select
    Range("a" & i * 4 - 6 + n).Select
Wend
End Sub


Regards,
DILIPandey
 
Upvote 0
Hi nopph,

use below macro:-

Code:
Sub datasplit()
'created by DILIPandey on 23-Feb'16
Range("a2").Select
i = 2
n = 0
s = ActiveSheet.Name
While ActiveCell.Value <> ""
    Range(Range("a" & i * 4 - 6 + n), Range("a" & i * 4 - 6 + n).Offset(4, 1)).Copy
    Sheets.Add
    Range("a2").PasteSpecial
    Range("a1").Value = "Name"
    Range("b1").Value = "Cost"
        i = i + 1
        n = n + 1
    Sheets(s).Select
    Range("a" & i * 4 - 6 + n).Select
Wend
End Sub


Regards,
DILIPandey
Dear DILIPandey,

Thank you so much, it's working.
Due to i'm a newbie, could you please explain about the "* 4 - 6" inside the code.
I guess that 6 is maximum rows per sheet.

Thank you.
 
Upvote 0
You are welcome.

(i * 4 - 6) is there to tell Excel about the starting position of the range to be copied and i is a running number here.
Run the code in break mode (F8) and move your cursor on these values to understand the logic completely.

Regards,
DILIPandey
 
Upvote 0
Thank you DLIPandey,

the F8(Code break mode) is give me better understanding about your code,

...Since in my real life have to adapt your code to work with around 1000000 records(Excel2013) and save them as Excel97-2003(6XXXX rec limited).

Could you please suggest me how to edit your code to split the data into 60000 records per sheet(i guess it's about the "Offset") ?

Thank you :) and sorry for asking too much :(
 
Upvote 0
Yes... you are right.
Offset(4, 1) is the place where you need to update.
So if you need to consider 60000 rows,
Offset(4,1) will change to Offset(599998,1)

Why 599998 ? because 60000-2 = 599998 where 2 is the starting row to copy the data (row 1 being heading).


Regards,
DILIPandey
 
Upvote 0
Dear DILIPandey

Thank you so much for your answers & explanations.
you are such a great teacher :)

Kind regards,
Noppph
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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