Created
November 8, 2021 13:04
-
-
Save siayi/b0fbfa3b4fe8725738e29a17d05e4d95 to your computer and use it in GitHub Desktop.
Split Data Excel by Row
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub SplitData() | |
Dim wb As Workbook | |
Dim ThisSheet As Worksheet | |
Dim NumOfColumns As Integer | |
Dim RangeToCopy As Range | |
Dim RangeOfHeader As Range 'data (range) of header row | |
Dim WorkbookCounter As Integer | |
Dim RowsInFile 'how many rows (incl. header) in new files? | |
Application.ScreenUpdating = False | |
'Initialize data | |
Set ThisSheet = ThisWorkbook.ActiveSheet | |
NumOfColumns = ThisSheet.UsedRange.Columns.Count | |
WorkbookCounter = 1 | |
RowsInFile = 5000 'as your example, just 5000 rows per file | |
'Copy the data of the first row (header) | |
Set RangeOfHeader = ThisSheet.Range(ThisSheet.Cells(1, 1), ThisSheet.Cells(1, NumOfColumns)) | |
For p = 2 To ThisSheet.UsedRange.Rows.Count Step RowsInFile - 1 | |
Set wb = Workbooks.Add | |
'Paste the header row in new file | |
RangeOfHeader.Copy wb.Sheets(1).Range("A1") | |
'Paste the chunk of rows for this file | |
Set RangeToCopy = ThisSheet.Range(ThisSheet.Cells(p, 1), ThisSheet.Cells(p + RowsInFile - 2, NumOfColumns)) | |
RangeToCopy.Copy wb.Sheets(1).Range("A2") | |
'Save the new workbook, and close it | |
wb.SaveAs ThisWorkbook.Path & "\5000" & WorkbookCounter | |
wb.Close | |
'Increment file counter | |
WorkbookCounter = WorkbookCounter + 1 | |
Next p | |
Application.ScreenUpdating = True | |
Set wb = Nothing | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment