I'm building a Macro
to import a file into Excel. At some point, when the file's location is requested, if the Worksheet user cancels the import option, the following error message is returned:
Excel can not find the text file to refresh this external data range
After this the execution of the macro is terminated. Checking Debug
. the error occurs in the .Refresh BackgroundQuery:=False
parameter of the procedure.
Below the import code:
Public Sub btnImportarTXT_Click()
' Sub para importar arquivo .TXT
Dim Dir As String
dirArquivo = Application.GetOpenFilename(FileFilter:="Text File, *.txt")
With Sheets("Extract").QueryTables.Add(Connection:="TEXT;" & dirArquivo, Destination:=Range("Extract!$A$1"))
.Name = "extract"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
' Worksheets("Extract").Protect UserInterfaceOnly:=True
MsgBox "Arquivo importado!", vbOKOnly, "Sucesso!"
End Sub