0

I am working with Excel 2016 on a Windows 7 system. While importing data from a text file (csv, tsv, etc.), in the third step of the data import wizard, the data type for each column can be configured manually. The default data type in that wizard page is "General". How can I change that default to "text" (or to other data type)?

0

It's possible to change the default data type from General to Text, but VBA is required.

This Macro will import a Text file into Excel and will also specify the data type as TEXT.

Sub Import()
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Test1.txt", Destination:=Range("$A$1") _
        )
        .Name = "Test1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(2, 2, 2)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

Note: Please check the line .TextFileColumnDataTypes = Array(2, 2, 2). Here 2 sets the TEXT format and the 3 ELEMENTS of the array specify 3 columns. You should set the array to have as many elements as the number of columns in your text file.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.