Results 1 to 4 of 4

Thread: [RESOLVED] write a 2d array to a .csv

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Location
    In the midst of corn, cotton, and beans
    Posts
    185

    Resolved [RESOLVED] write a 2d array to a .csv

    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

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Re: write a 2d array to a .csv

    Something like this:

    VB Code:
    1. 'a temp array (you can use your own array)
    2.     Dim arr(2, 2) As String
    3.     Dim i As Integer
    4.     Dim j As Integer
    5.     Dim strTemp As String
    6.     Dim strLine As String
    7.     Dim intFFN As Integer
    8.    
    9.     arr(0, 0) = 1
    10.     arr(0, 1) = 12
    11.     arr(0, 2) = 123
    12.     arr(1, 0) = "a"
    13.     arr(1, 1) = "ab"
    14.     arr(1, 2) = "abc"
    15.  
    16.  
    17.  
    18.     For i = 0 To UBound(arr, 1) - 1
    19.         For j = 0 To UBound(arr, 2)
    20.             If j = 0 Then
    21.                 strLine = strLine & arr(i, j)
    22.             Else
    23.                 strLine = strLine & "," & arr(i, j)
    24.             End If
    25.         Next
    26.        
    27.         strTemp = strTemp & strLine & vbCrLf
    28.         strLine = ""
    29.        
    30.     Next
    31.    
    32.     intFFN = FreeFile
    33.    
    34.     Open "c:\TempFile.csv" For Binary As intFFN
    35.    
    36.     Put #intFFN, , strTemp
    37.    
    38.     Close #intFFN

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: write a 2d array to a .csv

    repeated concatenation is slow, your better off doing the put statement in the loop.

  4. #4
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: write a 2d array to a .csv

    VB Code:
    1. Sub SaveCSV(array() As String, path As String, Optional Delim As String = ",", Optional quote As String = "")
    2. Dim opf As Long
    3. Dim row As Long
    4. Dim column As Long
    5.  
    6.     If Dir(path) <> "" Then Kill path
    7.  
    8.     opf = FreeFile
    9.     Open path For Binary As #opf
    10.  
    11.     For row = LBound(array,1) To UBound(array,1)
    12.         For column = LBound(array,2) To UBound(array,2)
    13.             Put #opf, , quote & array(row,column) & quote
    14.             If column < UBound(array,2) Then Put #opf, , delim
    15.         Next column
    16.         If row < UBound(array,1) Then Put #opf, , vbCrLf
    17.     Next row
    18.    
    19.     Close #opf
    20.  
    21. End Sub
    Last edited by jeroen79; Nov 1st, 2006 at 01:12 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width