Forum Discussion

AnayaZeeshan's avatar
AnayaZeeshan
Copper Contributor
Nov 06, 2021
Solved

Import utf 8 coded file in excel using VBA

Hello,
I am a beginner and having a trouble with this project. I am trying to import utf - 8 coded csv file into excel using VBA but its not working. I can import it manually using text data import option but when I try to use coding it imports the file but the characters doesn’t change and remain weird. What i want it to open utf coded csv using file picker. Any help would be greatly appreciated as I am really stuck and have a tight deadline.
Thanks
  • AnayaZeeshan 

     

     

    Import CSV with Unicode / UTF-8

    VBA code is untested

     

    Sub DataImport()
    'Copy the data from the CSV into the Excel
        Dim DateiName As String
        Dim ReplacePrep As String
        Dim LineFromFile As String
        Dim LineItems As Variant
        Dim row_number As Long
    
        Dim objStream As Object
    
        DateiName = "C:\Users\YOU\Documents\Nikolino\textino.txt"
    
        Set objStream = CreateObject("ADODB.Stream")
        objStream.Charset = "utf-8"
        objStream.Open
        objStream.LoadFromFile (DateiName)
    
        row_number = 10
    
        Do Until objStream.EOS                       'Go through the entire text document
            LineFromFile = objStream.ReadText(-2)    'Read line from source file
            LineItems = Split(LineFromFile, ",")
    
            Worksheets("Sheet1").Cells(row_number, 1).Value = LineItems(3)
            Worksheets("Sheet1").Cells(row_number, 2).Value = LineItems(5)
            Worksheets("Sheet1").Cells(row_number, 3).Value = LineItems(6)
            Worksheets("Sheet1").Cells(row_number, 4).Value = LineItems(7)
    
            row_number = row_number + 1
    
        Loop
        Set objStream = Nothing
    
    End Sub

     

     

    Hope I was able to help you with this info.

     

    NikolinoDE

    I know I don't know anything (Socrates)

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    AnayaZeeshan 

     

     

    Import CSV with Unicode / UTF-8

    VBA code is untested

     

    Sub DataImport()
    'Copy the data from the CSV into the Excel
        Dim DateiName As String
        Dim ReplacePrep As String
        Dim LineFromFile As String
        Dim LineItems As Variant
        Dim row_number As Long
    
        Dim objStream As Object
    
        DateiName = "C:\Users\YOU\Documents\Nikolino\textino.txt"
    
        Set objStream = CreateObject("ADODB.Stream")
        objStream.Charset = "utf-8"
        objStream.Open
        objStream.LoadFromFile (DateiName)
    
        row_number = 10
    
        Do Until objStream.EOS                       'Go through the entire text document
            LineFromFile = objStream.ReadText(-2)    'Read line from source file
            LineItems = Split(LineFromFile, ",")
    
            Worksheets("Sheet1").Cells(row_number, 1).Value = LineItems(3)
            Worksheets("Sheet1").Cells(row_number, 2).Value = LineItems(5)
            Worksheets("Sheet1").Cells(row_number, 3).Value = LineItems(6)
            Worksheets("Sheet1").Cells(row_number, 4).Value = LineItems(7)
    
            row_number = row_number + 1
    
        Loop
        Set objStream = Nothing
    
    End Sub

     

     

    Hope I was able to help you with this info.

     

    NikolinoDE

    I know I don't know anything (Socrates)

    • john1895's avatar
      john1895
      Copper Contributor

      NikolinoDE This is very helpful! I had trouble because i used FileSystemObject and cannot read my import data in UTF-8. Now with your help, my macro can properly import my data files! 

      • NikolinoDE's avatar
        NikolinoDE
        Gold Contributor
        I am glad that I could help you with your project.
        I wish you continued success with Excel!

        Please Mark and like this answer if it helps you as it will be beneficial for more community members reading here.

Resources