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 o...
  • NikolinoDE's avatar
    Nov 08, 2021

    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)

Resources