Import CSV (UTF-8) to Excel without formatting

mrwad

New Member
Joined
Oct 16, 2018
Messages
47
I have code for pasting data from CSV file to Worksheet. Everyhting works fine but I am facing some problems with current solution.


Is there a way to paste data as it is without formatting? Now Excel is formatting for example -Name to =-Name so there #Name ? errors instead of Values. Also it is formatting 215067910018 to 2,15068E+11.


Here is my current code:


Code:
Sub GetCustomers()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets.Add(After:= _
             ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    ws.Name = "Sheet1"


    Dim strText As String


    Dim file As String
    file = "L:\15\186507.CSV


    ' read utf-8 file to strText variable
   With CreateObject("ADODB.Stream")
        .Open
        .Type = 1  ' Private Const adTypeBinary = 1
        .LoadFromFile file
        .Type = 2  ' Private Const adTypeText = 2
        .Charset = "utf-8"
        strText = .ReadText(-1)  ' Private Const adReadAll = -1
    End With


    ' parse strText data to a sheet
    intRow = 1
    For Each strLine In Split(strText, Chr(10))
        If strLine <> "" Then
            With ws
                .Cells(intRow, 1) = strLine
                .Cells(intRow, 1).TextToColumns Destination:=Cells(intRow, 1), DataType:=xlDelimited, _
                    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                    Semicolon:=False, Comma:=True, Space:=False, Other:=False
            End With


            intRow = intRow + 1
        End If
    Next strLine

Asked also here: https://stackoverflow.com/questions/58426479/import-csv-utf-8-to-excel-without-formatting
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

mrwad

New Member
Joined
Oct 16, 2018
Messages
47
Well this works if run after import:


Code:
ThisWorkbook.Worksheets("Sheet1").Range("A1:K900").NumberFormat = "@"


but since during the import:


Code:
    ' parse strText data to a sheet
    intRow = 1
    For Each strLine In Split(strText, Chr(10))
        If strLine <> "" Then
            With ws
                .Cells(intRow, 1) = strLine
                .Cells(intRow, 1).TextToColumns Destination:=Cells(intRow, 1), DataType:=xlDelimited, _
                    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                    Semicolon:=False, Comma:=True, Space:=False, Other:=False
            End With


            intRow = intRow + 1
        End If
    Next strLine


Excel already added "=-Name" it does not change to "-Name" and still gives an error. I can't figure out how to set


Code:
ThisWorkbook.Worksheets("Finland").Range("A1:K900").NumberFormat = "@"


during import and not after everything has been done.
 

Forum statistics

Threads
1,148,390
Messages
5,746,432
Members
424,016
Latest member
Shizzl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top