VBA Paste Data to New Worksheet If Row Number Exceeds 101

Captain_Conman

Board Regular
Joined
Jun 14, 2018
Messages
54
I currently have a worksheet with 277 rows of data. (One header and 276 transactions). I would like to limit each worksheet to 101 rows of data. (Header and 100 transactions). For example, instead of having 277 rows in Sheet 1, I would like Sheet 1 to have 101 rows (header and 100 transactions), Sheet 2 to have 101 rows (header and 100 transactions), and sheet 3 to have 77 rows (header and remaining 76 transactions). Ideally, this will work and make sheets regardless of how many transactions there are. For example, it would work if there are 276 or 884 etc.

Finally, I would like the sheet to be named based on the number of rows. In this example...
Sheet 1 = 101
Sheet 2 = 101
Sheet 3 = 77

Thank you in advance for help, I hope I laid my problem out okay.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Sounds like a job for a nested loop!

You'll need 2 counter variables, one to count the total number of rows copied and one to count to 100.

Create separate subs for the following:
- Copy headers
- Copy rows
- Create new sheet

Use a Do Until loop to increment each copied row onto a separate sheet. Define your limit as the last row within your source sheet. Within THAT loop, you could use a Do While loop to increment up to your desired dataset length (100) before calling your "Create new sheet" routine to create a new sheet and continue the loop onto the next sheet.
 
Last edited:
Upvote 0
You can't have more than one sheet with the same name.
Maybe something along the lines of this and then delete the original sheet

Code:
Sub DataToSheets()
    Dim src As Worksheet
    Dim lr As Long, lc As Long, x As Long
    Dim HeadArray As Variant, DataArray As Variant
    
Set src = ThisWorkbook.Sheets("Data")     '<~~~ alter sheet name as required
Application.ScreenUpdating = False
With src
    lr = .Cells(Rows.Count, 1).End(xlUp).Row
    lc = .Cells(1, Columns.Count).End(xlToLeft).Column
    HeadArray = .Range("A1").Resize(, lc).Value
    For x = 2 To lr Step 100
        DataArray = .Range("A" & x).Resize(100, lc).Value
        Sheets.Add After:=Worksheets(Worksheets.Count)
        ActiveSheet.Cells(1, 1).Resize(, lc) = HeadArray
        ActiveSheet.Cells(2, 1).Resize(100, lc) = DataArray
        ActiveSheet.Name = ActiveSheet.Name & " (" & ActiveSheet.UsedRange.Rows.Count - 1 & " Transactions)"
    Next x
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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