OK, how would you write an array to a .csv?
e.g.
stored in Array(x,x) as string
Array(0,0) = 1
Array(0,1) = 12
Array(0,2) = 123
Array(1,0) = a
Array(1,1) = ab
Array(1,3) = abc
Output to .csv
1,12,123
a,ab,abc
Printable View
OK, how would you write an array to a .csv?
e.g.
stored in Array(x,x) as string
Array(0,0) = 1
Array(0,1) = 12
Array(0,2) = 123
Array(1,0) = a
Array(1,1) = ab
Array(1,3) = abc
Output to .csv
1,12,123
a,ab,abc
Something like this:
VB Code:
'a temp array (you can use your own array) Dim arr(2, 2) As String Dim i As Integer Dim j As Integer Dim strTemp As String Dim strLine As String Dim intFFN As Integer arr(0, 0) = 1 arr(0, 1) = 12 arr(0, 2) = 123 arr(1, 0) = "a" arr(1, 1) = "ab" arr(1, 2) = "abc" For i = 0 To UBound(arr, 1) - 1 For j = 0 To UBound(arr, 2) If j = 0 Then strLine = strLine & arr(i, j) Else strLine = strLine & "," & arr(i, j) End If Next strTemp = strTemp & strLine & vbCrLf strLine = "" Next intFFN = FreeFile Open "c:\TempFile.csv" For Binary As intFFN Put #intFFN, , strTemp Close #intFFN
repeated concatenation is slow, your better off doing the put statement in the loop.
VB Code:
Sub SaveCSV(array() As String, path As String, Optional Delim As String = ",", Optional quote As String = "") Dim opf As Long Dim row As Long Dim column As Long If Dir(path) <> "" Then Kill path opf = FreeFile Open path For Binary As #opf For row = LBound(array,1) To UBound(array,1) For column = LBound(array,2) To UBound(array,2) Put #opf, , quote & array(row,column) & quote If column < UBound(array,2) Then Put #opf, , delim Next column If row < UBound(array,1) Then Put #opf, , vbCrLf Next row Close #opf End Sub