LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default 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
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I tried to get around the problem of the pivot table field settingdefaulting to Count instead of Sum by running a macro of change the settingfrom Count to Sum. However, when I tried to run the Macro, I got error messageof run time error 1004, unable Enda80 Excel Discussion (Misc queries) 1 May 3rd 08 10:52 AM
QueryTable date problem Tom Excel Programming 0 May 3rd 06 02:43 PM
Problem:Query Cell contain Formula from QueryTable is always get old value Resant Excel Programming 1 March 3rd 06 07:28 AM
Populate QueryTable Using SQL Server - Connection Problem AMIT Excel Programming 1 September 7th 05 09:34 AM
Selection.QueryTable problem JenC Excel Programming 2 January 7th 05 05:32 AM


All times are GMT +1. The time now is 06:38 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"