0

Good day!

In VBA I have to write into a CSV file a large array. To increase speed of writing I want to write a line at once to avoid looping over each field. The solution with looping works. The solution with line at once inserts more or less quotes (") then it is required.

Dim WrdArray() As String
Dim ArrayOut(10) As String 'Required for 10000000 rows
Dim line As String
line = "A|B|C|D|E|F|Z|H|I|K|L|M|N|O|P|Q|R|S|T|V|X"

'Simulate content
For i = 1 To 10
    ArrayOut(i) = line
Next

'->SOLUTION 1 line in loop
Filename = "C:" & "\Temp\line_in_loop.csv"
Open Filename For Output As #1

For r = LBound(ArrayOut) To UBound(ArrayOut)
    WrdArray() = Split(ArrayOut(r), "|")
    For i = LBound(WrdArray) To UBound(WrdArray) 'This loop is time consuming
        If i < UBound(WrdArray) Then
            Write #1, WrdArray(i),
        Else
            Write #1, WrdArray(i)
        End If
    Next
Next
Close #1

'line_in_loop.csv: "A","B","C","D" ...
'That is correct
'<-SOLUTION 1 line in loop

'->SOLUTION 2 line at once

Filename = "C:" & "\Temp\line_at_once.csv"
Open Filename For Output As #1
For i = 1 To UBound(ArrayOut)
    line = Replace(ArrayOut(i), "|", Chr(34) & "," & Chr(34))
    Write #1, "V1:", line
    line = Replace(ArrayOut(i), "|", ",")
    Write #1, "V2:", line
Next
Close #1

'line_at_once.csv: "V1:","A"",""B"",""C"",""D"",""E""...
'line_at_once.csv: "V2:","A,B,C,D,E ...

'How to provide write line_at_once.csv in format?: "A","B","C","D" ...

'<-SOLUTION 2 line at once

The code is provided here, CSV_line_at_once.accdb, Module1.

How to write line at once in line_at_once.csv in following CSV format?: "A","B","C","D" ...

Please help.

Thanks.

'->SOLUTION 3 line at once - SOLVED as answered below!
Filename = "C:" & "\Temp\line_at_once.csv"
Open Filename For Output As #1
For i = 1 To UBound(ArrayOut)
line = Replace(ArrayOut(i), "|", Chr(34) & "," & Chr(34))
line = Chr(34) & line & Chr(34)
Print #1, line
Next
Close #1
'<-SOLUTION 3 line at once - SOLVED as answered below!

I found also an additional solution with FSO and looping by lines.

https://technet.microsoft.com/en-us/library/ee198715.aspx

'->SOLUTION 4 Save CSV, line loop with FSO
'Dim WrdArray() As String
Dim ArrayOut(10) As String 'Required for 10000000 rows
Dim line As String
line = "A|B|C|D|E|F|Z|H|I|K|L|M|N|O|P|Q|R|S|T|V|X"

'Simulate content
For i = 1 To UBound(ArrayOut)
    ArrayOut(i) = line
Next

'https://technet.microsoft.com/en-us/library/ee198715.aspx
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\Temp\fso_line_loop.csv")
For i = 1 To UBound(ArrayOut)
    line = ArrayOut(i)
    line = Replace(line, "|", Chr(34) & "," & Chr(34))
    line = Chr(34) & line & Chr(34)
    ArrayOut(i) = line
    objFile.WriteLine (ArrayOut(i))
Next
objFile.Close
'<-SOLUTION 4 Save CSV, line loop with FSO

I am still looking for the fastest way which I think is saving at once the entire array ArrayOut() without looping.

The below code SOLUTION 5 does not work.

Please help.

'->SOLUTION 5 Does not work. Please help.
Print #1, ArrayOut() 'case A
objFile.WriteLine (ArrayOut()) 'case B
itsergiu
  • 662
  • 6
  • 11
  • you should use print instead of write. Write tends to put extra quotes :) – Doug Coats May 26 '17 at 14:03
  • 1
    I added: line = Chr(34) & line & Chr(34). Print #1, line. It works! – itsergiu May 26 '17 at 14:16
  • glad you figured it out – Doug Coats May 26 '17 at 14:21
  • If you are looking for performance with large volume, you should stop using VBA. It is very slow compared to the speed you can achieve using VB.NET (which is free) – braX Jun 16 '17 at 12:26
  • Is this question still open? It's a little unclear. If yes, what is the actual, current question? – Andre Jun 16 '17 at 19:07
  • This question is still open. Solutions 1-4 solve the issue but in loop, record by record. It works but it is slow for large data. The code is necessary in VBA because it is part of a MS Access. To save a file with 10 millions records takes 10-15 minute and generates a file of 100 MB. The PC is very fast CPU I7, SSD it copies in File Explorer a file of 100 MB in a second, on the other side generation of 100 MB takes 10-15 minutes. To avoid loop, I am asking how the entire ArrayOut() can be copied at once in a CSV(text file). – itsergiu Jun 19 '17 at 09:07

0 Answers0