#If VBA7 Then
'Maps a character string to a UTF-16 (wide character) string
Private Declare PtrSafe Function MultiByteToWideChar Lib "kernel32" _
(ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As LongPtr, ByVal cchMultiByte As Long, ByVal lpWideCharStr As LongPtr, ByVal cchWideChar As Long) As Long
#Else
Private Declare Function MultiByteToWideChar Lib "kernel32" _
(ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
#End If
'UTF-8 code page
Private Const CP_UTF8 = 65001
Public Sub Convert_UTF8_CSV_File_To_Workbook()
Dim csvFile As String
Dim csvWorkbook As Workbook
Dim fileNum As Integer
Dim UTF8csvLine As String, VBAstring As String
Dim r As Long, csvParts As Variant
csvFile = "C:\path\to\UTF8 file.csv" 'CHANGE THIS
Application.ScreenUpdating = False
Set csvWorkbook = Workbooks.Add(xlWBATWorksheet)
r = 1
fileNum = FreeFile
Open csvFile For Input As #fileNum
While Not EOF(fileNum)
Line Input #fileNum, UTF8csvLine
VBAstring = UTF8StringToVBAString(UTF8csvLine)
csvParts = Split(VBAstring, ",")
csvWorkbook.Worksheets(1).Cells(r, 1).Resize(, UBound(csvParts) + 1).Value = csvParts
r = r + 1
Wend
Close #fileNum
Application.DisplayAlerts = False 'suppress warning if .csv file already exists - the file is overwritten
csvWorkbook.SaveAs Filename:=Replace(csvFile, ".csv", ".xlsx", Compare:=vbTextCompare), FileFormat:=xlOpenXMLWorkbook
csvWorkbook.Close False
Application.DisplayAlerts = True
Application.ScreenUpdating = False
End Sub
'Use MultiByteToWideChar API function to convert a string of UTF-8 characters to a VBA (Unicode) string
Private Function UTF8StringToVBAString(ByRef UTF8string As String) As String
Dim UTF8bytes() As Byte
Dim bufferSize As Long
UTF8bytes = StrConv(UTF8string, vbFromUnicode)
'Get required size of output string
bufferSize = MultiByteToWideChar(CP_UTF8, 0, VarPtr(UTF8bytes(0)), UBound(UTF8bytes) + 1, 0, 0)
'Allocate output string
UTF8StringToVBAString = String$(bufferSize, 0)
'Convert UTF8 bytes to Unicode output string
MultiByteToWideChar CP_UTF8, 0, VarPtr(UTF8bytes(0)), UBound(UTF8bytes) + 1, StrPtr(UTF8StringToVBAString), bufferSize
End Function