Anthony White
Anthony White

Reputation: 44

Write Chinese Characters And Alphabet in Cells Into .CSV

I'm trying to write data into a CSV file. The data contains both Chinese Characters and general text. My results look like this after exporting it into CSV file:

enter image description here

But it is supposed to look like this:

enter image description here

All the Chinese Characters, like "物料申请系统", "ADC培训", etc. are all turned into "?".

Here is my code:

Open Location For Output As #1
    For i = 2 To lastrow
        For j = 1 To LastCol
            If j = LastCol Then 'keep writing to same line
                TextLine = TextLine & Cells(i, j).Text  'read line into variable
            Else 'end the line
                TextLine = TextLine & Cells(i, j).Text & Deliminator
            End If
        Next j
            Print #1, TextLine
            TextLine = ""
    Next i
Close #1

Upvotes: 0

Views: 548

Answers (2)

Anthony White
Anthony White

Reputation: 44

Thanks to @Ron Rosenfeld for suggesting FileSystemObject. I was able to generate the CSV file with both Chinese Characters and general text without any problem with the following code that I amended:

With CreateObject("Scripting.FileSystemObject")
  With .CreateTextFile(Location, , True)
    For i = 2 To lastrow
        For j = 1 To LastCol
            If j = LastCol Then
                TextLine = TextLine & Cells(i, j).Text
            Else
                TextLine = TextLine & Cells(i, j).Text & Deliminator
            End If
        Next j
            .WriteLine TextLine
            TextLine = ""
    Next i
        .Close
  End With
End With

Upvotes: 1

Ron Rosenfeld
Ron Rosenfeld

Reputation: 60224

Try something like:

 your_worksheet.SaveAs "your file path and name", xlUnicodeText

You could also use the FileSystemObject

I don't believe the VBA Open for Output : Print (or Write) support Unicode.

Upvotes: 0

Related Questions