Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Check if a directory exists and if it doesn t...

Hi,

To open a file in a directory (C:\Export Archive\yyyymmdd\) with dat
day-1 I use the follwing code :

variablefile = Range("E1").Value

Workbooks.Open Filename:= _
"C:\Export Archive\" & Format(Now() - 1, "yyyymmdd") & "\"
"Export" & variablefile & ".xls"

(a directory is saved every day from monday to friday)

My code works pretty good, exept on monday because it looks for th
directory day-1. And as there is no directory saved with sunday's o
saturday's date, it doesn t work. Same problem with holidays.

So, I would like to improve my code to do something like "open th
directory with date day-1. If the directory does not exist, try day-2
or day-3,... until it finds the directory with the most recent date.

Could you help me to do that please ?

Maybe something like setting the date as variable and increasing it b
doing a loop until it finds a directory with the same date ?

Thank you very much in advance for you help,

Gre

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 79
Default Check if a directory exists and if it doesn t...

Use the Dir() function. It returns "" if the file doesn't exist, so
something like

myFile = Dir(pathname...)
If myFile = "" then

HTH, Greg

"Grek " wrote in message
...
Hi,

To open a file in a directory (C:\Export Archive\yyyymmdd\) with date
day-1 I use the follwing code :

variablefile = Range("E1").Value

Workbooks.Open Filename:= _
"C:\Export Archive\" & Format(Now() - 1, "yyyymmdd") & "\" &
"Export" & variablefile & ".xls"

(a directory is saved every day from monday to friday)

My code works pretty good, exept on monday because it looks for the
directory day-1. And as there is no directory saved with sunday's or
saturday's date, it doesn t work. Same problem with holidays.

So, I would like to improve my code to do something like "open the
directory with date day-1. If the directory does not exist, try day-2,
or day-3,... until it finds the directory with the most recent date.

Could you help me to do that please ?

Maybe something like setting the date as variable and increasing it by
doing a loop until it finds a directory with the same date ?

Thank you very much in advance for you help,

Greg


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Check if a directory exists and if it doesn t...

Thank you for your help Steve.

I ve updated my code but unfortunately it keeps looping.

For my test i created a workbook gregtest.xls in c:\Greg\
In this directory I have 2 others directory 20040508 and 20040506. So
the code is supposed to open the file test.xls in the director
20040506

Here is the updated code :

*******************

mydate = Now() - 1

mydir = Dir("c:\Greg\" & Format(mydate, "yyyymmdd"))

Do Until mydir < ""
mydate = mydate - 1
mydir = Dir("c:\Greg\" & Format(mydate, "yyyymmdd"))
Loop

Workbooks.Open Filename:= _
mydir & "\" & "test.xls"

******************

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Check if a directory exists and if it doesn t...

Sorry, didn't look at your code as a whole

Try
mydir = Dir("c:\Greg\" & Format(mydate, "yyyymmdd"), vbDirectory)
in both cases

Grek < wrote:
Thank you for your help Steve.

I ve updated my code but unfortunately it keeps looping.

For my test i created a workbook gregtest.xls in c:\Greg\
In this directory I have 2 others directory 20040508 and 20040506. So,
the code is supposed to open the file test.xls in the directory
20040506

Here is the updated code :

*******************

mydate = Now() - 1

mydir = Dir("c:\Greg\" & Format(mydate, "yyyymmdd"))

Do Until mydir < ""
mydate = mydate - 1
mydir = Dir("c:\Greg\" & Format(mydate, "yyyymmdd"))
Loop

Workbooks.Open Filename:= _
mydir & "\" & "test.xls"

*******************


---
Message posted from http://www.ExcelForum.com/


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Check if a directory exists and if it doesn t...

So, for the sake of completeness, this should be a version of your code
which actually works

Dim myDate As Date, myDir As String
myDate = Now() - 1

myDir = Dir("c:\Greg\" & Format(myDate, "yyyymmdd"), vbDirectory)

Do Until myDir < ""
myDate = myDate - 1
myDir = Dir("c:\Greg\" & Format(myDate, "yyyymmdd"), vbDirectory)
Loop

Workbooks.Open Filename:="c:\Greg\" & myDir & "\" & "test.xls"



Steve Garman wrote:

Sorry, didn't look at your code as a whole

Try
mydir = Dir("c:\Greg\" & Format(mydate, "yyyymmdd"), vbDirectory)
in both cases

Grek < wrote:

Thank you for your help Steve.

I ve updated my code but unfortunately it keeps looping.

For my test i created a workbook gregtest.xls in c:\Greg\
In this directory I have 2 others directory 20040508 and 20040506. So,
the code is supposed to open the file test.xls in the directory
20040506

Here is the updated code :

*******************

mydate = Now() - 1

mydir = Dir("c:\Greg\" & Format(mydate, "yyyymmdd"))

Do Until mydir < ""
mydate = mydate - 1
mydir = Dir("c:\Greg\" & Format(mydate, "yyyymmdd"))
Loop

Workbooks.Open Filename:= _
mydir & "\" & "test.xls"

*******************


---
Message posted from http://www.ExcelForum.com/





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Check if a directory exists and if it doesn t...

GREAT, the code works perfectly now !
Thanks a lot !

Actually, why the vbDirectory is so important in the code and why i
didn t work without that ?

Thanks again,

Gre

--
Message posted from http://www.ExcelForum.com

Reply
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
How to check if the directory exists? ira Excel Programming 2 January 19th 04 01:22 PM
Check if directory empty OR no of files in directory. Michael Beckinsale Excel Programming 2 December 4th 03 10:12 PM
Check whether a worksheet exists already clui[_6_] Excel Programming 2 December 3rd 03 05:19 PM
Verify a directory exists MacroMan[_4_] Excel Programming 3 August 8th 03 04:38 PM
check if sheet exists Ross[_6_] Excel Programming 3 July 25th 03 06:46 PM


All times are GMT +1. The time now is 04:12 PM.

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"