Date question

J

JT

My macro is embedded in an Excel workbook. On 'Sheet1' there are 2 input
fields, (1) the 'Start' date, and (2) the 'End' date (for a 1 week period).
Users will enter both dates.

There are several restrictions: (1) the 'Start' date must be a Sunday, (2)
the 'End' date must be a Saturday. and (3) these dates can only be for a 1
week period (7 days). There are edits already in place to identify issues
for these 3 items.

It will also be okay for users to enter a week in the future. However, they
cannot enter last week's dates.

For example, as of today, (5/2/10 and 5/8/10), (5/9/10 and 5/15), and
(5/16/10 and 5/22/10) are all acceptable ranges.

However, (4/25/10 and 5/1/10) is not an acceptable range because it starts
in a prior week. Any Sunday 'Start' date prior to 5/2/10 is not allowed and
I need an edit to trap this error.

The user can run the macro any day in the current week for the current week
and any week in the future. They just cannot run it for prior weeks.

I think I'm looking for code that will tell me what the date is for the
current week's Sunday and then I can compare that to the 'Start' date to see
if is prior to the date for the current week's Sunday (unless I'm making this
too difficult and there is an easier way to do this.)

Thanks for the help.
 
F

FSt1

hi
see if you can use this.......
MsgBox Date - Weekday(Date, 2)
MsgBox WeekdayName(Weekday(Date, 1) - Weekday(Date, 2))
If [A1].Value < Date - Weekday(Date, 2) Then
MsgBox "In the past!!! Abort! Abort!"
End If

also read up on the weekday function in vb help.
regards
FSt1
 
H

Helmut Meukel

Here is a function you can use:

'*************************************************************
'* Procedure Name: PriorDay1 *
'*-----------------------------------------------------------*
'* Created: ??.??.???? By: Microsoft *
'* published in BasicPro 3/96 in NeatCode.mdb, obviously *
'* copied from Microsoft Knowledge Base (see Q185480) *
'* Modified: 21.03.2001 By: HM-Soft Hof, Helmut Meukel *
'*===========================================================*
'* returns the date of the last DayCode (1=Sun ... 7=Sat) *
'* on or before the date D. *
'* You should use one of the VbDayOfWeek constants *
'* (e.g. VbMonday) as actual value for DayCode. *
'* *
'*************************************************************
Public Function PriorDay1(ByVal D As Date, ByVal DayCode As VbDayOfWeek) As Date
PriorDay1 = D - Weekday(D) + DayCode - IIf(Weekday(D) >= DayCode, 0, 7)
End Function

Helmut.
 
R

Ron Rosenfeld

My macro is embedded in an Excel workbook. On 'Sheet1' there are 2 input
fields, (1) the 'Start' date, and (2) the 'End' date (for a 1 week period).
Users will enter both dates.

There are several restrictions: (1) the 'Start' date must be a Sunday, (2)
the 'End' date must be a Saturday. and (3) these dates can only be for a 1
week period (7 days). There are edits already in place to identify issues
for these 3 items.

It will also be okay for users to enter a week in the future. However, they
cannot enter last week's dates.

For example, as of today, (5/2/10 and 5/8/10), (5/9/10 and 5/15), and
(5/16/10 and 5/22/10) are all acceptable ranges.

However, (4/25/10 and 5/1/10) is not an acceptable range because it starts
in a prior week. Any Sunday 'Start' date prior to 5/2/10 is not allowed and
I need an edit to trap this error.

The user can run the macro any day in the current week for the current week
and any week in the future. They just cannot run it for prior weeks.

I think I'm looking for code that will tell me what the date is for the
current week's Sunday and then I can compare that to the 'Start' date to see
if is prior to the date for the current week's Sunday (unless I'm making this
too difficult and there is an easier way to do this.)

Thanks for the help.

Why must the user enter BOTH dates?

If I needed to restrict the entry dates, without knowing what else you are
doing, I would use Data Validation for the Start date, and I would compute the
End Date.

For Data Validation, I would use, assuming your Start Date is in A2:

Data/Data Validation
Settings: Custom
Formula: =AND(WEEKDAY(A2)=1,A2>(TODAY()-WEEKDAY(TODAY())))
You can then put appropriate messages and prompts in the other tabs.

Then for the end date, it would be simply:

=if(a2="","",a2+6)

--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top