I'm creating a VB Excel Macro code that will help me to append 2 CSV files, but the problem is, the old CSV contains some Japanese texts, so after running through the code / script, the new CSV file contains garbage texts
Can anyone kindly help me or point out what's wrong with my code?
Sub Append2CSV()
Dim tmpCSV As String 'string to hold the CSV info
Dim f As Integer
Dim csvFile As String
Set data_sheet = ThisWorkbook.Worksheets("Test")
LastColumnCell = data_sheet.Cells(Rows.Count, "A").End(xlUp).row
csvFile = Range("E3").Value
f = FreeFile
Open csvFile For Append As #f
tmpCSV = Range2CSV(Range("A5:CZ" & LastColumnCell))
Print #f, tmpCSV
Close #f
MsgBox "Test Data is appended in the Static Resource succesfully.", vbOKOnly, "Static Resource Update"
End Sub
Function Range2CSV(list As Range) As String
Dim data As Variant
Dim r As Long, c As Long
Dim csvRecord As String, csvEmptyFields As String
Dim csvAll As String
csvAll = ""
data = list.Value
For r = 1 To UBound(data)
csvEmptyFields = ""
csvRecord = ""
For c = 1 To UBound(data, 2)
If Not IsEmpty(data(r, c)) Then
csvRecord = csvRecord & csvEmptyFields & data(r, c) & ","
csvEmptyFields = ""
Else
csvEmptyFields = csvEmptyFields & ","
End If
Next
If csvRecord <> "" Then csvAll = csvAll & Left(csvRecord, Len(csvRecord) - 1) & vbLf
Next
Range2CSV = Left(csvAll, Len(csvAll) - 1)
End Function
Sample Screenshots
red line: existing cases (not garbage)
blue line: appended data (garbage)
I've tried the comment FsT is not working, and the Garbage data still appears
Open csvFile For Append As #f
tmpCSV = Range2CSV(Range("A5:CZ" & LastColumnCell))
On Error GoTo errHandler
Dim fsT As Object
Dim tFilePath As String
tFilePath = csvFile + ".csv"
Set fsT = CreateObject("ADODB.Stream")
fsT.Type = 2 'Specify stream type - we want To save text/string data.
fsT.Charset = "utf-8" 'Specify charset For the source text data.
fsT.Open 'Open the stream And write binary data To the object
fsT.WriteText tmpCSV
fsT.SaveToFile tFilePath, 2 'Save binary data To disk
errHandler:
Close #f