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.
My CSV strings look like:
123456,"Word1","Word2","-Word3",,"Word4","Word5","00076",,"Word7","Word8"
After running the code "-Word3" looks like #Name? in Excel cell. Also instead of 00076 I get 76
Here is my current 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
End Sub
EDIT:
I am able to do it while Converting Text to Columns manually. Shouldn't it also be possible by VBA? I mean set Column Data Format while .TextToColumns

TextToColumnsmethod specifiesTextQualifier=xlDoubleQuote. Are things like -Name in double quotes? If there were, that problem might go away?Power Query(available in Excel 2010+), it respects the double quote and imports"-Wordn"as a text string.