Array to CSV, output characters different to array

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm writing contents of an array to a .CSV file. When I open the .CSV file, the contents are mostly symbolic characters rather than alphabetic. I can't work out what is causing this, my guess is I'm not writing the contents to the file correctly.

The array is 46 rows by 13 columns and contains a mix of strings and doubles.

The code I'm using to write the array to a .CSV file is:
Code:
Private Sub Save_Results(ByRef varArrData() As Variant)


    Dim strSavePath     As String
    Dim strFileName     As String
    
    Dim x               As Long
    Dim y               As Long
    Dim lngFileNum      As Long
    
    Const strDelim      As String = ","
    
    lngFileNum = FreeFile
    
    strSavePath = "C:\test\"
    strSavePath = strSavePath & varArrData(2, UBound(varArrData, 2))
    
    Open strSavePath For Binary As lngFileNum
    
    For x = LBound(varArrData, 1) To UBound(varArrData, 1)
        For y = LBound(varArrData, 2) To UBound(varArrData, 2)
            Put lngFileNum, , varArrData(x, y)
            If y < UBound(varArrData, 2) Then Put lngFileNum, , strDelim
        Next y
        If x < UBound(varArrData, 1) Then Put lngFileNum, , vbCrLf
    Next x
    
    Close lngFileNum
    
End Sub
Can anyone suggest why the output is not being written as expected?

TIA,
JackDanIce
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
That's because you're writing the file in binary mode. Try the following instead...

Code:
    Open strSavePath For Output As lngFileNum
    
    For x = LBound(varArrData, 1) To UBound(varArrData, 1)
        For y = LBound(varArrData, 2) To UBound(varArrData, 2)
            If y < UBound(varArrData, 2) Then
                Write [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=lngFileNum]#lngFileNum[/URL] , varArrData(x, y);
            Else
                Write [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=lngFileNum]#lngFileNum[/URL] , varArrData(x, y)
            End If
        Next y
    Next x
    
    Close lngFileNum

Hope this helps!
 
Upvote 0
Hi Domenic, I missed your reply yesterday but worked it out this morning, yes indeed is for Output, thank you for replying and confirming!
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,023
Latest member
alabaz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top