NS Admin
08/25/2022, 6:18 PMSub splitFileGS()
Dim ACS As Range, Z As Long, New_WB As Workbook, _
Total_Columns As Long, Start_Row As Long, Stop_Row As Long, Copied_Range As Range
Dim Headers() As Variant
Set ACS = ActiveSheet.UsedRange
With ACS
Headers = .Rows(1).Value
Total_Columns = .Columns.Count
End With
Start_Row = 2
Do While Stop_Row <= ACS.Rows.Count
Z = Z + 1
If Z > 1 Then Start_Row = Stop_Row + 1
Stop_Row = Start_Row + 24500 'edit your record count here. Mine is 24,500 rows per file.
With ACS.Rows
If Stop_Row > .Count Then Stop_Row = .Count
End With
With ACS
Set Copied_Range = .Range(.Cells(Start_Row, 1), .Cells(Stop_Row, Total_Columns))
End With
Set New_WB = Workbooks.Add
With New_WB
With .Worksheets(1)
.Cells(1, 1).Resize(1, Total_Columns) = Headers
.Cells(2, 1).Resize(Copied_Range.Rows.Count, Total_Columns) = Copied_Range.Value
End With
'can find more file types here: <https://docs.microsoft.com/en-us/office/vba/api/excel.xlfileformat>
'you will need extension: .csv .xlsx etc and file format id. 6 is .csv
'you can change filename by editing in the quotes. e.g. file-z. z is the count
.SaveAs ACS.Parent.Parent.Path & Application.PathSeparator & "file-" & Z & ".csv", FileFormat:=6
.Close
End With
If Stop_Row = ACS.Rows.Count Then Exit Do
Loop
End Sub