Jeff Johnson
New Member
- Joined
- May 20, 2015
- Messages
- 9
Hi,
I am trying to automatically import all text files (csv type) from a given folder. My code to pick the folder and loop through the files is working but the code (which was recorded during Get External Data) that is supposed to drop the data in my workbook is throwing an error:
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 removed or renamed, then try the refresh again.
Here's a subset of my code:
'Name new sheet
Sheets(ActiveSheet.Name).Name = dest_sheet_name
' Open .s2p files in selected folder and import them all
With Sheets(dest_sheet_name).QueryTables.Add(Connection:= _
"TEXT;" + file_name, Destination:=Sheets(dest_sheet_name).Range("$a$2"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
The .Refresh BackgroundQuery isn't working.
Thanks for any help!
I am trying to automatically import all text files (csv type) from a given folder. My code to pick the folder and loop through the files is working but the code (which was recorded during Get External Data) that is supposed to drop the data in my workbook is throwing an error:
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 removed or renamed, then try the refresh again.
Here's a subset of my code:
'Name new sheet
Sheets(ActiveSheet.Name).Name = dest_sheet_name
' Open .s2p files in selected folder and import them all
With Sheets(dest_sheet_name).QueryTables.Add(Connection:= _
"TEXT;" + file_name, Destination:=Sheets(dest_sheet_name).Range("$a$2"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
The .Refresh BackgroundQuery isn't working.
Thanks for any help!