![]() |
QueryTable problem when running first time only
For the code below, I get the following "Run-Time error '1004'"
"Excel cannot find the text file to refresh this external data range. Check to make sure that the text file has not been moved or renamed, then try the refresh again." What I can't understand is why the code works fine when I run the program immediately after receiving the error. It works this way everytime I go to import another new text file which makes me think it might have something to do with the default file location being set after the error occurs (?). Thanks for any insights, -Tony Sub GetWFiles(Wpath As String, WhichWfile As Integer) Dim i As Long, qt As QueryTable, Wdat As String, OrigWSheet As String 'On Error Resume Next Worksheets.Add After:=Worksheets("OrigTarr") OrigWSheet = "OrigW" If WhichWfile < 1 Then OrigWSheet = OrigWSheet & CStr(WhichWfile) ActiveSheet.Name = OrigWSheet 'this is where the error occurs first time through only Wdat = Dir(Wpath & "\W*.dat") Do While Wdat = "" GetWDirectoryAgain Wpath, WhichWfile Wdat = Dir(Wpath & "W*.dat") Loop i = 0 Do While Wdat < "" i = i + 1 Cells(1, i).Value = Wdat With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;" & Wpath & Wdat, Destination:=Cells(2, i)) .Name = Left(Wdat, Len(Wdat) - 4) .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 = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Wdat = Dir() For Each qt In ActiveSheet.QueryTables qt.Delete Next Loop End Sub |
All times are GMT +1. The time now is 12:25 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com