LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 51
Default I want Excel to determine the correct directory.

I thought I should share a solution to a small bug in my previous macro. The
code that I thought was complete ended up having a bug. If the Month or Year
happens to be different between Today, Friday and Saturday, meaning the month
or year changes within the week, then the value assignments for Friday,
Saturday, Month and/or Year end up being wrong.

For example, if I run the previous macro on Monday, March 31st, 2008, the
value for the month of the coming Friday and Saturday get assigned 35 and 36,
not 03 and 04, the 3rd and 4th of April. Same with the month and then even
the year if you happen to run this macro in the last week of the year and the
year is different between the day you run it and the year of the coming
Friday and Saturday. I will post my new macro below.

Dim FriMonth, SatMonth, FriYear, SatYear, Fri, Sat, Zero, MyWeekday
Zero = 0

' What day of the week is it today?
MyWeekday = Weekday(Now(), 1)
If MyWeekday = 2 Then GoTo Line1001: ' Today is Monday
If MyWeekday = 3 Then GoTo Line1002: ' Today is Tuesday
If MyWeekday = 4 Then GoTo Line1003: ' Today is Wednesday
If MyWeekday = 5 Then GoTo Line1004: ' Today is Thursday
If MyWeekday = 6 Then GoTo Line1005: ' Today is Friday

Line1001:

' Monday

Fri = Day(Now() + 4) ' Friday's Number
Sat = Day(Now() + 5) ' Saturday's Number
If Sat < 10 Then Sat = Zero & Sat
If Fri < 10 Then Fri = Zero & Fri

'Month

FriMonth = Month(Now() + 4) ' Friday's Month
SatMonth = Month(Now() + 5) ' Saturday's Month
If FriMonth < 10 Then FriMonth = Zero & FriMonth
If SatMonth < 10 Then SatMonth = Zero & SatMonth

' Year
FriYear = Right(Year(Now() + 4), 2) ' Friday's year
SatYear = Right(Year(Now() + 5), 2) ' Saturday's year

GoTo Line1006:

Line1002:

' Tuesday

Fri = Day(Now() + 3) ' Friday's Number
Sat = Day(Now() + 4) ' Saturday's Number
If Sat < 10 Then Sat = Zero & Sat
If Fri < 10 Then Fri = Zero & Fri

'Month

FriMonth = Month(Now() + 3) ' Friday's Month
SatMonth = Month(Now() + 4) ' Saturday's Month
If FriMonth < 10 Then FriMonth = Zero & FriMonth
If SatMonth < 10 Then SatMonth = Zero & SatMonth

' Year
FriYear = Right(Year(Now() + 3), 2) ' Friday's year
SatYear = Right(Year(Now() + 4), 2) ' Saturday's year

GoTo Line1006:

Line1003:

' Wednesday

Fri = Day(Now() + 2) ' Friday's Number
Sat = Day(Now() + 3) ' Saturday's Number
If Sat < 10 Then Sat = Zero & Sat
If Fri < 10 Then Fri = Zero & Fri

'Month

FriMonth = Month(Now() + 2) ' Friday's Month
SatMonth = Month(Now() + 3) ' Saturday's Month
If FriMonth < 10 Then FriMonth = Zero & FriMonth
If SatMonth < 10 Then SatMonth = Zero & SatMonth

' Year
FriYear = Right(Year(Now() + 2), 2) ' Friday's year
SatYear = Right(Year(Now() + 3), 2) ' Saturday's year

GoTo Line1006:

Line1004:

' Thursday

Fri = Day(Now() + 1) ' Friday's Number
Sat = Day(Now() + 2) ' Saturday's Number
If Sat < 10 Then Sat = Zero & Sat
If Fri < 10 Then Fri = Zero & Fri

'Month

FriMonth = Month(Now() + 1) ' Friday's Month
SatMonth = Month(Now() + 2) ' Saturday's Month
If FriMonth < 10 Then FriMonth = Zero & FriMonth
If SatMonth < 10 Then SatMonth = Zero & SatMonth

' Year
FriYear = Right(Year(Now() + 1), 2) ' Friday's year
SatYear = Right(Year(Now() + 2), 2) ' Saturday's year

GoTo Line1006:

Line1005:

' Friday

Fri = Day(Now()) ' Friday's Number
Sat = Day(Now() + 1) ' Saturday's Number
If Sat < 10 Then Sat = Zero & Sat
If Fri < 10 Then Fri = Zero & Fri

'Month

FriMonth = Month(Now()) ' Friday's Month
SatMonth = Month(Now() + 1) ' Saturday's Month
If FriMonth < 10 Then FriMonth = Zero & FriMonth
If SatMonth < 10 Then SatMonth = Zero & SatMonth

' Year
FriYear = Right(Year(Now()), 2) ' Friday's year
SatYear = Right(Year(Now() + 1), 2) ' Saturday's year

Line1006:

' Import this weeks Work Order & Wrap Work Order
ChDir "\\fileserver\" & SatYear & SatMonth & Sat
Workbooks.Open
"\\FileServer\"&SatYear&SatMonth&Sat&"\P"&FriMonth &Fri&FriYear&".XLS")
 
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 correct cannot access directory 'J:\\'. ex_usmc_doc Excel Discussion (Misc queries) 1 February 1st 10 03:09 PM
How to determine if a Folder/Directory Exists in Excel VBA [email protected] Excel Programming 2 November 17th 06 02:38 AM
How to determine the parent directory KS Wong[_2_] Excel Programming 5 May 6th 05 02:27 AM
Pointing to the Correct Directory Rodg2000 Excel Programming 2 October 28th 03 04:56 AM
How does Excel determine the TEMP directory? Miso Excel Programming 3 July 31st 03 02:19 PM


All times are GMT +1. The time now is 02:56 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"