VBA for importing particular columns of CSV using Query

I have the following code below for importing a csv file. How can i modify it to import only selected columns of the csv file. (say columns 1,3 and 5 only)?

 

Dim xFileName As Variant

    xFileName = Application.GetOpenFilename(“CSV File (*.csv), *.csv”, , “Browse the File S2KEventMsg_Table.csv”, , False)

    If xFileName = False Then Exit Sub

    On Error Resume Next

 

With ActiveSheet.QueryTables.Add(“TEXT;” & xFileName, Range(“A2”))

        .FieldNames = True

        .RowNumbers = False

        .FillAdjacentFormulas = False

        .PreserveFormatting = True

        .RefreshOnFileOpen = False

        .RefreshStyle = xlInsertDeleteCells

        .SavePassword = False

        .SaveData = True

        .RefreshPeriod = 0

        .TextFilePromptOnRefresh = False

        .TextFilePlatform = 936

        .TextFileStartRow = 1

        .TextFileParseType = xlDelimited

        .TextFileTextQualifier = xlTextQualifierDoubleQuote

        .TextFileConsecutiveDelimiter = False

        .TextFileTabDelimiter = True

        .TextFileSemicolonDelimiter = False

        .TextFileCommaDelimiter = True

        .TextFileSpaceDelimiter = False

        .TextFileTrailingMinusNumbers = True

        .Refresh BackgroundQuery:=False

By: Jyothis

Leave a Reply

Your email address will not be published. Required fields are marked *