View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Error can't Find File

Or you could just build the name correctly.

Dim myDate as date
dim myFileName as string
dim wkbk as workbook
dim myPath as string

mypath = "C:\yourpathhere\" 'include the trailing backslash

mydate = date - 1 'yesterday

'fix the extension to match .xlsx or .xlsm or ...
myfilename = "myfile" & format(mydate, "mmmmyyyy") & ".xls"

set wkbk = nothing
on error resume next
set wkbk = workbooks.open(filename:=mypath & myfilename)
on error goto 0

if wkbk is nothing then
msgbox myfilename & " wasn't found in: " & mypath
exit sub
end if

....code to work on wkbk goes here



Dave wrote:

Hi,

I have a macro that opens a file and pulls any outstanding tasks from the
previous day.

I have a problem when it is a new month because the previous month's file is
called MyFileMarch2010.

The macro now looks for MyFileApril2010. How do I capture the error and look
for the previous month's file if it is the first business day of the month?

Any help or suggestions is appreciated.
--
Thank you

Dave


--

Dave Peterson