PC Review


Reply
Thread Tools Rate Thread

Date/Day VBA Calculation

 
 
Rob
Guest
Posts: n/a
 
      17th Mar 2009
I was wondering how I can get VBA in Excel'03 to recognize that if today is
Tuesday the 17th, or Firday the 20th, then the previous Monday must have been
the 16th. Or is it is Friday April the 3rd then the previous Monday was
March 30th? Escentially I am just looking for a way to have the VB identify
what Day today is as well as the date and then calculate what the previous
Monday was... Unless the current day happens to actually be Monday, in which
case it just uses that date.

Dim tDate, mDate As Date

tDate = Format(Now(),"m/d/yyyy")
tDay = Day(tDate)

If tDay <> "Monday" Then mDate = tDate - tDay

The above code it what I toyed with but it has failed at every itteration I
made of it.


Thanks In Advance!
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      17th Mar 2009
Rob,

Try this

tDate = Date - WeekDay(Date, 2) + 1

For future work you simply change the last 1 to a 2 for Tuesday etc.

Mike

"Rob" wrote:

> I was wondering how I can get VBA in Excel'03 to recognize that if today is
> Tuesday the 17th, or Firday the 20th, then the previous Monday must have been
> the 16th. Or is it is Friday April the 3rd then the previous Monday was
> March 30th? Escentially I am just looking for a way to have the VB identify
> what Day today is as well as the date and then calculate what the previous
> Monday was... Unless the current day happens to actually be Monday, in which
> case it just uses that date.
>
> Dim tDate, mDate As Date
>
> tDate = Format(Now(),"m/d/yyyy")
> tDay = Day(tDate)
>
> If tDay <> "Monday" Then mDate = tDate - tDay
>
> The above code it what I toyed with but it has failed at every itteration I
> made of it.
>
>
> Thanks In Advance!

 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      17th Mar 2009
And including your formatting

tDate = Format(Date - WeekDay(Date, 2) + 1, "mm/dd/yyyy")

Mike

"Mike H" wrote:

> Rob,
>
> Try this
>
> tDate = Date - WeekDay(Date, 2) + 1
>
> For future work you simply change the last 1 to a 2 for Tuesday etc.
>
> Mike
>
> "Rob" wrote:
>
> > I was wondering how I can get VBA in Excel'03 to recognize that if today is
> > Tuesday the 17th, or Firday the 20th, then the previous Monday must have been
> > the 16th. Or is it is Friday April the 3rd then the previous Monday was
> > March 30th? Escentially I am just looking for a way to have the VB identify
> > what Day today is as well as the date and then calculate what the previous
> > Monday was... Unless the current day happens to actually be Monday, in which
> > case it just uses that date.
> >
> > Dim tDate, mDate As Date
> >
> > tDate = Format(Now(),"m/d/yyyy")
> > tDay = Day(tDate)
> >
> > If tDay <> "Monday" Then mDate = tDate - tDay
> >
> > The above code it what I toyed with but it has failed at every itteration I
> > made of it.
> >
> >
> > Thanks In Advance!

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      17th Mar 2009
Dear Rob, please try this

If this helps click Yes
---------------
Jacob Skaria

Dim varDate,monDate
varDate = Date
monDate = Format(varDate - (IIf(Weekday(varDate) >= vbMonday,
Weekday(varDate) - vbMonday, 7 - (vbMonday - Weekday(varDate)))))

 
Reply With Quote
 
Rob
Guest
Posts: n/a
 
      17th Mar 2009
That's a Beautiful solution!!! Thanks Greatly!!



"Mike H" wrote:

> Rob,
>
> Try this
>
> tDate = Date - WeekDay(Date, 2) + 1
>
> For future work you simply change the last 1 to a 2 for Tuesday etc.
>
> Mike
>
> "Rob" wrote:
>
> > I was wondering how I can get VBA in Excel'03 to recognize that if today is
> > Tuesday the 17th, or Firday the 20th, then the previous Monday must have been
> > the 16th. Or is it is Friday April the 3rd then the previous Monday was
> > March 30th? Escentially I am just looking for a way to have the VB identify
> > what Day today is as well as the date and then calculate what the previous
> > Monday was... Unless the current day happens to actually be Monday, in which
> > case it just uses that date.
> >
> > Dim tDate, mDate As Date
> >
> > tDate = Format(Now(),"m/d/yyyy")
> > tDay = Day(tDate)
> >
> > If tDay <> "Monday" Then mDate = tDate - tDay
> >
> > The above code it what I toyed with but it has failed at every itteration I
> > made of it.
> >
> >
> > Thanks In Advance!

 
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
A specific date used for calculation of a new date Ditte Microsoft Excel Misc 7 17th Jul 2009 08:00 AM
Date Calculation (from entered date / 1yr later in next field) ajaminb Microsoft Excel Worksheet Functions 5 29th Sep 2008 02:11 PM
End Date Calculation (adding a start date duration) Silena K-K Microsoft Excel Misc 5 25th Jan 2008 04:27 PM
Tricky Date calculation: How to calculate a future date chriswessels@xtra.co.nz Microsoft Excel Misc 9 11th Aug 2006 04:24 AM
Coding to show first _weekday_ prior to a date, when date calculation happens to fall on weekend? StargateFan Microsoft Excel Programming 5 9th Dec 2004 09:06 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:16 AM.