Hello everyone!
I have recorded a simple macro that imports text from a csv file, divides it in columns using the "," separator and pastes it in the A1 cell of my workbook. Simple. Then I have added some lines to prompt the user to select the file that will be imported.
Dim fNameAndPath As Variant
fNameAndPath = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv), *.csv", title:="Select File To Be Opened")
If fNameAndPath = False Then Exit Sub
MsgBox (fNameAndPath)
'Workbooks.Open Filename:=fNameAndPath
'Insert data from txt file on desktop
With ActiveSheet.QueryTables.Add(Connection:="TEXT;fNameAndPath", Destination:=Range("$A$1"))
.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 = True
.TextFileSemicolonDelimiter = True
.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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=True
End With
The problem is that I get an error on the last line of the With: ".Refresh BackgroundQuery:=True" and I don't know why. It says "Run-time error '1004': Excel cannot find the text file to refresh this external data range. Check to make sure the text file has not been moved or renamed, then try the refresh again."
I have done some research on Google and I find people with similar problems but not exactly the same thing.
I am specially annoyed because this code worked on Friday before leaving the office but will not work now...
Thanks in advance for any tips you might give me.
Bookmarks