Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created November 9, 2012 14:44
Show Gist options
  • Select an option

  • Save wpsmith/4046082 to your computer and use it in GitHub Desktop.

Select an option

Save wpsmith/4046082 to your computer and use it in GitHub Desktop.
VBA: Split CSV by rows of 25
Sub Split()
Dim rLastCell As Range
Dim rCells As Range
Dim strName As String
Dim lLoop As Long, lCopy As Long
Dim wbNew As Workbook
With ThisWorkbook.Sheets(1)
Set rLastCell = .Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious)
For lLoop = 2 To rLastCell.Row Step 200
lCopy = lCopy + 1
Set wbNew = Workbooks.Add
.Cells(1, 1).EntireRow.Copy _
Destination:=wbNew.Sheets(1).Range("A1")
.Range(.Cells(lLoop, 1), .Cells(lLoop + 199, .Columns.Count)).EntireRow.Copy _
Destination:=wbNew.Sheets(1).Range("A2")
wbNew.SaveAs Filename:="Inventory_" & Format(lLoop + 199, "0000") & ".csv", FileFormat:=xlCSV, CreateBackup:=False, Local:=True
wbNew.Close SaveChanges:=False
Next lLoop
End With
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment