Split data in a sheet by specific number of rows using VBA in Microsoft Excel

In this article, we will create a macro to split data by specific number of rows.

We have raw data in sheet “RawData”. We want to split this data in multiple sheets.

ArrowRawData

ArrowMain

Before running the macro, we need to specify the number of rows required in each sheet.

ArrowOutput

Code explanation

CntRows = Cint(Sheets("Main").TextBox1.Value)

The above code is used to get the count of number of sheets required in a sheet.

LastRow = .Range("A" & .Rows.Count).End(xlUp).Row

The above code is used to get the row number of the last cell.

Sheets.Add after:=Sheets(Sheets.Count)

The above code is used to add a new sheet after the last sheet.

.Range("A" & n).Resize(CntRows, LastColumn).Copy Range("A1")

The above code is used to copy specified number of rows to a new worksheet.

 

Please follow below for the code


Option Explicit

Sub SplitDataToMultipleSheets()

'Declaring variables
Dim LastRow As Long, n As Long, CntRows As Long
Dim LastColumn As Integer

'Getting count of number of rows required in one sheet
CntRows = CInt(Sheets("Main").TextBox1.Value)

'Disabling screen updates
Application.ScreenUpdating = False

With Sheets("RawData")
    
    'Getting row number and column number of last cell
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    LastColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
    
    'Looping through data in the sheet
    For n = 1 To LastRow Step CntRows
        
        'Adding new worksheet
        Sheets.Add after:=Sheets(Sheets.Count)
        'Copying data to new worksheet
        .Range("A" & n).Resize(CntRows, LastColumn).Copy Range("A1")
    Next n
    
    .Activate
End With

'Enabling screen updates
Application.ScreenUpdating = True

End Sub

 

If you liked this blog, share it with your friends on Facebook. Also, you can follow us on Twitter and Facebook.

We would love to hear from you, do let us know how we can improve our work and make it better for you. Write to us at info@exceltip.com

Comments

  1. I have tried this a few times and I'm getting errors. Basically I have a list with 15000 rows and I would like to create 150 new lists of 100 rows each. Is that even possible?

    Thanks
    Stephen

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.