[FONT=Courier New][SIZE=1]Option Explicit[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Sub MultipleCSV_Limit_v2()[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1] Const sFolder As String = "[COLOR=red]C:\TEMP\[/COLOR]" [/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green]' where CSV files will be written
[/COLOR] Const sPrefix As String = "[COLOR=red]File_[/COLOR][COLOR=black]"[/COLOR] [/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green]' root of CSV file name
[/COLOR] Const iMaxRecords As Long = [COLOR=red]500[/COLOR] [/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green]' how many records in each CSV file
[/COLOR] Const iPadFactor As Integer = [COLOR=red]4[/COLOR] [/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green] ' how many digits in file name serial number
[/COLOR] Const iDataStart As Integer = [COLOR=black]2[/COLOR] [/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green]' where the data starts on the worksheet
[/COLOR]
Dim ws As Worksheet
Dim iLastRow As Long
Dim iLastCol As Long
Dim iRow As Long
Dim iCol As Long
Dim sColumnHeads As String
Dim iSerialNo As Integer
Dim intFH As Integer
Dim iRecordNo As Long
Dim sFilename As String
Dim dtStart As Date
dtStart = Now()
Set ws = ThisWorkbook.Sheets("[COLOR=red]Sheet1[/COLOR]")
iLastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
[/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=blue] If iDataStart > 1 Then
iLastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
For iCol = 1 To iLastCol
sColumnHeads = sColumnHeads & ",""" & ws.Cells(1, iCol) & """"
Next iCol
sColumnHeads = Mid(sColumnHeads, 2)
End If
[/COLOR]
iSerialNo = 0
iRow = iDataStart
Close
Do Until iRow > iLastRow
iSerialNo = iSerialNo + 1
sFilename = sFolder & sPrefix & Right(String(iPadFactor, "0") & CStr(iSerialNo), iPadFactor) & ".csv"
intFH = FreeFile()
iRecordNo = 0
Open sFilename For Output As intFH
[/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=blue] If iDataStart > 1 Then
Print #intFH, sColumnHeads
End If
[/COLOR] Do Until iRecordNo = iMaxRecords Or iRow > iLastRow
Print #intFH, Format(ws.Cells(iRow, 1), "dd/mm/yyyy"); ","; [/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green]' how to output a date
[/COLOR] Print #intFH, """"; ws.Cells(iRow, 2); ""","; [/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green]' how to output a string
[/COLOR] Print #intFH, ws.Cells(iRow, 3) [/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=green]' how to output a number
[/COLOR] iRecordNo = iRecordNo + 1
iRow = iRow + 1
Loop
Close intFH
Loop[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1] MsgBox "Done: " & CStr(iLastRow - iDataStart + 1) & " records in worksheet" & Space(15) _
& vbCrLf & vbCrLf _
& CStr(iSerialNo) & " file" & IIf(iSerialNo = 1, "", "s") & " created in " & sFolder & Space(15) _
& vbCrLf & vbCrLf _
& "Run time: " & Format(Now() - dtStart, "hh:nn:ss"), vbOKOnly + vbInformation[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1]End Sub[/SIZE][/FONT]