VBA Export to CSV UTF-8 - without comma delimiter

nd0911

Board Regular
Joined
Jan 1, 2014
Messages
166
Hello,

I have a code that export data to a CSV UTF-8 file (using ADO.stream library).

The code works well but there is one problem, if the text I export has a comma (,) it considers it as a new column.

How can I perform this task without the CSV file thinking that the commas I have in the text are supposed to break the text into different columns ?

This is the code:

VBA Code:
Sub CreateCSVinUTF8(wS As Worksheet, FilePath As String)
Dim ByteData()  As Byte
Dim Text        As String
Dim vaInput()   As Variant
Dim Y As Long
Dim X As Integer

Const adCrLf                As Long = -1
Const adModeUnknown         As Long = 0
Const adSaveCreateOverWrite As Long = 2
Const adTypeText            As Long = 2
    
vaInput = wS.UsedRange

For Y = 1 To UBound(vaInput, 1)
    For X = 1 To UBound(vaInput, 2)
        Text = Text & vaInput(Y, X) & ","
    Next X
    Text = Left(Text, Len(Text) - 1) & vbCrLf
Next Y

With CreateObject("ADODB.Stream")
    .Mode = adModeUnknown
    .Type = adTypeText
    .LineSeparator = adCrLf
    .Charset = "UTF-8"
    .Open
    .WriteText Text
    .SaveToFile FilePath, adSaveCreateOverWrite
    .Close
End With
            
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hello,​
as CSV means 'comma separated values' if your data have any comma in any column you must export those columns delimited by double quotes​
like for example this 3 columns row 1,"Hell, Marc",2021/5/5 …​
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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