Jarryd Moore
New Member
- Joined
- Jan 23, 2019
- Messages
- 1
I'm trying to create a macro that will take the current sheet and split it into multiple CSV files, batched at 1000 lines each.
The below code is what I have. It's saving the files but each file contains the same data (row 2 to row 1001). I need each file to be a batch of the whole set of data (ie. row 2 to row 1001, row 1002 to row 2001, row 2002 to 3001, etc).
What have I got wrong?
The below code is what I have. It's saving the files but each file contains the same data (row 2 to row 1001). I need each file to be a batch of the whole set of data (ie. row 2 to row 1001, row 1002 to row 2001, row 2002 to 3001, etc).
What have I got wrong?
Code:
Sub Split_1000_With_Column_Headings()
Dim inputFile As String, inputWb As Workbook
Dim lastRow As Long, row As Long, n As Long
Dim newCSV As Workbook
inputFile = Application.ThisWorkbook.FullName
Set inputWb = Workbooks.Open(inputFile)
With inputWb.ActiveSheet
lastRow = .Cells(Rows.Count, "A").End(xlUp).row
Set newCSV = Workbooks.Add
n = 0
For row = 2 To lastRow Step 1000
n = n + 1
.Rows(1).EntireRow.Copy newCSV.ActiveSheet.Range("A1")
.Rows(row & ":" & row + 1000 - 1).EntireRow.Copy newCSV.ActiveSheet.Range("A2")
'Save in same folder as input workbook with .xlsx replaced by (n).csv
newCSV.SaveAs Filename:=inputWb.Path & "\Exports\" & "Contacts Import for Xero " & Format(Date, "yyyymmdd") & " (" & n & ").csv", FileFormat:=xlCSV, CreateBackup:=False
Next
End With
newCSV.Close saveChanges:=False
inputWb.Close saveChanges:=""
End Sub