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.
Before running the macro, we need to specify the number of rows required in each sheet.
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.
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
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 firstname.lastname@example.org
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.