Problem importing text file via VBA

1

I'm trying to import a text file into excel through the following macro:

Sub Teste()

    Dim intervalo As String
    intervalo = "intervalo"

    Workbooks.Add
    ActiveWorkbook.Sheets("Plan1").Select

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

    Range("intervalo").Select

    ActiveWorkbook.SaveAs Filename:="testeCSV"
    ActiveWorkbook.Close

End Sub

But when debugging, when I get to .CommandType = 1 , an error occurs and I can not identify the problem.

    
asked by anonymous 10.07.2015 / 21:31

1 answer

1

You should not use CommandType to import text files, as is the case with you.

See the Microsoft Translated Help:

  

You can set the CommandType property only if the value of the QueryType property of the lookup table or the PivotTable cache is xlOLEDBQuery.

Note that, as I said, this is not your case, since you are not doing a query in a database, for example.

    
11.07.2015 / 14:23