2
I’m building a Macro
for importing a file in Excel. At some point, when the file location is requested, in case the Worksheet cancel import option, the following error message is returned:
Excel cannot find the text file to refresh this External data range
After that the macro execution is terminated. Checking the Debug
. the error occurs in the parameter .Refresh BackgroundQuery:=False
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
And the code used to cancel the import?
– Caffé
@Caffé, there is no code. When the
dirArquivo = Application.GetOpenFilename(FileFilter:="Text File, *.txt")
, is opened aDialog
on Windows to select the file. When I cancel thisDialog
, returns the error reported above.– Fellipe Soares