Alright, so, basically I have an XSLM file containing about ~40k rows. I need to export these rows to a customized CSV format - ^ delimited and ~ marking the boundaries of each cell. Once they've been exported, they are read in by a Joomla importer app and processed into the database. I found a good macro script which does just that and tweaked it to use the correct delimiters.
Sub CSVFile()
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
'ListSep = Application.International(xlListSeparator)
ListSep = "^" ' Use ^ as field separator.
If Selection.Cells.Count > 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
CurrTextStr = ìî
For Each CurrCell In CurrRow.Cells
CurrTextStr = CurrTextStr & "~" & CurrCell.Value & "~" & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End Sub
However, what've I've found is that the generated CSVs are simply too big to be handled with the available script execution time. I can split the files manually to about 5000 rows apiece and it does well enough. What I'd like to do is adjust the above script as follows:
- Stores the header row to be inserted into each file.
- Asks the user how many rows should be output per file.
- Appends -pt# to the chosen save as file name.
- Processes out the Excel file into as many 'chunk' csv files as required.
For example, if my file name was output, the file break number was 5000, and the excel file had 14000 rows, I'd end up with output-pt1.csv, output-pt2.csv, and output-pt3.csv.
If it were just me doing it, I'd just keep breaking the files manually, but when all is said and done I need to hand these files off to the client commissioning the project, so the easier the better.
Much appreciated for any ideas.