Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional formula to open file

Status
Not open for further replies.

noexit

Technical User
Feb 4, 2008
3
US
I have a file name saved by date, and I'd like to make a conditional formula to check to see if there's a file name with the current date in it, and if not, open the previous day's file. This code will open the two files.

Code:
Sub OpenFile()

Dim strYesterdayFile As String
Dim strTodayFile As String

strFileName = "File " & Format(DateAdd("d", -1, Date), "mm.dd.yy") & ".xls"
strTodayFile = "File " & Format(Now(), "mm.dd.yy") & ".xls"

Workbooks.Open Filename:=strYesterdayFile
Workbooks.Open Filename:=strTodayFile

Sub End

How do I use an If Then statement to try to open today's file, and if it doesn't exist, then open yesterday's file? To complicate things further, it would also be nice to skip weekends, so on Monday, to open Friday's file.
 




Hi,

Try a loop
Code:
Sub OpenFile()
    Dim i As Integer
    Dim strYesterdayFile As String
    Dim strTodayFile As String
    
    On Error Resume Next
    
    i = -1
    Do
        Err.Clear
        strFileName = "File " & Format(DateAdd("d", i, Date), "mm.dd.yy") & ".xls"
        Workbooks.Open Filename:=strFileName
        i = i - 1
    Loop While Err.Number <> 0
    
    strTodayFile = "File " & Format(Now(), "mm.dd.yy") & ".xls"
    Workbooks.Open Filename:=strTodayFile
End Sub


Skip,

[glasses]Did you hear what happened when the OO programmer lost his library?...
He's now living in OBJECT poverty![tongue]
 
create a FileSystemObject:
Code:
Set fs = CreateObject("Scripting.FileSystemObject")
Then use its FileExists method:
Code:
if fs.FileExists(strTodayFile) then
  Workbooks.Open Filename:=strTodayFile
else
  Workbooks.Open Filename:=strYesterdayFile
end if

_________________
Bob Rashkin
 
Bongs code works great. Now how about on a monday or after a holiday? On another project, I just made a input box pop up asking for the previous day's date, stored that as a string, and used it when the previous day's date was needed. That seems the simplest right now, but if I can find a more elegant way, I might as well try.
 



Did you try the loop?

Skip,

[glasses]Did you hear what happened when the OO programmer lost his library?...
He's now living in OBJECT poverty![tongue]
 
Why not simply play with the Dir function ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Okay, the loop worked, but the example above opened both today's date, and the previous date. I modified it as follows.

Code:
Sub OpenFile()
    Dim i As Integer
    Dim strFileName As String
    
    On Error Resume Next
    
    i = 0
    Do
        Err.Clear
        strFileName = "File " & Format(DateAdd("d", i, Date), "mm.dd.yy") & ".xls"
        Workbooks.Open Filename:=strFileName
        i = i - 1
    Loop While Err.Number <> 0
End Sub

Making i = 0 made it start with today's date, making the lines to open the current dated file at the end unnessisary.
 




Glad that the code worked for you.

Skip,

[glasses]Did you hear what happened when the OO programmer lost his library?...
He's now living in OBJECT poverty![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top