PC Review


Reply
Thread Tools Rate Thread

Cannot find file - code error

 
 
keri
Guest
Posts: n/a
 
      30th Jun 2007
Hi,

I have the code below that opens each file in a folder, finds a value
and returns it to another sheet. However I am having a problem with
the code.

rnum = 1
Dim wkb As Workbooks
Dim filnames
filnames = Dir(mypath & "*" & ".xls")
Do While filnames <> ""
Set mybook = Workbooks.Open(filnames, , True)
For Each sh In mybook.Worksheets
Set sourcerange = sh.Range("a1001000")
With sourcerange
Set rng = .Find(WHAT:="TOTAL")
If Not rng Is Nothing Then
basebook.Sheets("sheet1").cells(rnum, "B").Value = mybook.Name
basebook.Worksheets("SHEET1").cells(rnum, "C").Value =
rng.Offset(0, 1).Value
rnum = rnum + 1
Else
'do nothing
End If
End With
Next sh
mybook.Close savechanges:=False
Loop
End If

I get a runtime error '1004'
'ORDER21107.XLS' could not be found.Check the spelling of the file
name, and verify that the file location is correct.

(This debugs on the line
Set mybook = Workbooks.Open(filnames, , True))

I have checked the path is correct (and I know it must be as the error
is returning a filename in the correct folder) so it seems to be
another problem to this.

Any help would be appreciated before I pull all my hair out!

 
Reply With Quote
 
 
 
 
FM
Guest
Posts: n/a
 
      30th Jun 2007
If I'm not wrong, when you want to retreive all the files in a directory,
the Dir command should be launched only the first time with the parameters,
after you have to invoke it without parameters.

You should try to place the Dir command before the LOOP line

FM


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      30th Jun 2007
dir(...) will return the name of the file--not including the path.

Try changing:
Set mybook = Workbooks.Open(filnames, , True)
to
Set mybook = Workbooks.Open(mypath & filnames, , True)

Depending on what myPath is, it may work the way you want.

keri wrote:
>
> Hi,
>
> I have the code below that opens each file in a folder, finds a value
> and returns it to another sheet. However I am having a problem with
> the code.
>
> rnum = 1
> Dim wkb As Workbooks
> Dim filnames
> filnames = Dir(mypath & "*" & ".xls")
> Do While filnames <> ""
> Set mybook = Workbooks.Open(filnames, , True)
> For Each sh In mybook.Worksheets
> Set sourcerange = sh.Range("a1001000")
> With sourcerange
> Set rng = .Find(WHAT:="TOTAL")
> If Not rng Is Nothing Then
> basebook.Sheets("sheet1").cells(rnum, "B").Value = mybook.Name
> basebook.Worksheets("SHEET1").cells(rnum, "C").Value =
> rng.Offset(0, 1).Value
> rnum = rnum + 1
> Else
> 'do nothing
> End If
> End With
> Next sh
> mybook.Close savechanges:=False
> Loop
> End If
>
> I get a runtime error '1004'
> 'ORDER21107.XLS' could not be found.Check the spelling of the file
> name, and verify that the file location is correct.
>
> (This debugs on the line
> Set mybook = Workbooks.Open(filnames, , True))
>
> I have checked the path is correct (and I know it must be as the error
> is returning a filename in the correct folder) so it seems to be
> another problem to this.
>
> Any help would be appreciated before I pull all my hair out!


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA code could not find file MeSteve Microsoft Access Form Coding 3 12th Feb 2008 03:09 PM
Can't find Partial Code file Lucky Microsoft ASP .NET 3 14th Jun 2006 11:52 AM
'Circular reference' error but can't find such an error in the code. Richard Lewis Haggard Microsoft C# .NET 10 2nd May 2006 03:45 PM
Macro code can't find file =?Utf-8?B?Q2VjaWw=?= Microsoft Excel Programming 4 26th Mar 2004 07:45 AM
I cannot find a setup error code in the KB (error code is 4) Ed Becker Microsoft Windows 2000 Setup 1 29th Oct 2003 12:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:42 AM.