Walker Wayne
Walker Wayne

Reputation: 35

Changing Number type to Text type when import csv by vba

I got an import csv code below

Private Sub Workbook_Open()
 Dim xFileName As Variant
    Dim Rg As Range
    Dim xAddress As String
    xFileName = Application.GetOpenFilename("CSV File (*.csv), *.csv", , , , False)
    If xFileName = False Then Exit Sub
    On Error Resume Next
    xAddress = Range("A1").Address
    With ActiveSheet.QueryTables.Add("TEXT;" & xFileName, Range(xAddress))
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 65001
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

Normally, it worked but when import csv but some kind of column that has start with 0 number, then excel treat that cell as Number and delete ( hide ) all 0 begin cell.

I've tried to add this script in but that not work.

ActiveSheet.NumberFormat = "@"

Upvotes: 1

Views: 1817

Answers (1)

0m3r
0m3r

Reputation: 12499

Have you tried format the destination field

Example

QueryTable.TextFileColumnDataTypes property

.TextFileColumnDataTypes = Array(xlTextFormat)

Upvotes: 1

Related Questions