Problem importing text file via VBA

Asked

Viewed 1,011 times

1

I am trying to import a text file to 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 arriving at the .CommandType = 1 an error occurs and I’m not able to identify what the problem is.

inserir a descrição da imagem aqui

  • And what error it shows?

1 answer

1


You should not use Commandtype for importing text files, as is the case for you.

See Microsoft Translated Help:

You can set the Commandtype property only if the value of the Querytype property of the query table or the dynamic table 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.

  • Okay, I recorded the macro and didn’t even know where to start, but that was really the problem, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.