Advanced Dates?

M

macropod

Hi Doug,

How about:
Sub GetDate()
Dim Days As Integer, Weekday As Integer, EndDate As Date
Days = 90
Weekday = 6
EndDate = DateAdd("d", Days, Date)
EndDate = Int(EndDate / 7) * 7 - Weekday * (EndDate Mod 7 <> Weekday)
EndDate = EndDate - ((Format(EndDate, "d") < 7) + (Format(EndDate, "d") > 28) * 2 + _
(Format(EndDate, "d") > 14) * (Format(EndDate, "d") < 22)) * 7
MsgBox "The Date is " & Format(EndDate, "dddd, d MMM yyyy")
End Sub
 
D

Doug Robbins - Word MVP on news.microsoft.com

Hi Paul,

Not quite. It does not correctly handle when Date is a Saturday.

I haven't yet figured out what would need to be done to it to correct that.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

macropod said:
Hi Doug,

How about:
Sub GetDate()
Dim Days As Integer, Weekday As Integer, EndDate As Date
Days = 90
Weekday = 6
EndDate = DateAdd("d", Days, Date)
EndDate = Int(EndDate / 7) * 7 - Weekday * (EndDate Mod 7 <> Weekday)
EndDate = EndDate - ((Format(EndDate, "d") < 7) + (Format(EndDate, "d") >
28) * 2 + _
(Format(EndDate, "d") > 14) * (Format(EndDate, "d") < 22)) * 7
MsgBox "The Date is " & Format(EndDate, "dddd, d MMM yyyy")
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Doug Robbins - Word MVP on news.microsoft.com said:
How about the following in which the For j ... Next is just for testing

Dim today As Date, today1 As Date
Dim i As Long
Dim j As Long
today = Date

For j = 0 To 15
today1 = DateAdd("d", j, today)
Select Case Format(today1, "ddd")
Case "Sat"
i = 97
Case "Sun"
i = 96
Case "Mon"
i = 95
Case "Tue"
i = 94
Case "Wed"
i = 93
Case "Thu"
i = 92
Case Else
i = 91
End Select
Select Case Format(DateAdd("d", i, today1), "d")
Case Is < 8
i = i + 7
Case 15 To 20
i = i + 7
End Select
MsgBox "The Date is " & Format(DateAdd("d", i, today1), "dddd d MMMM
yyyy")
Next j


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

Doug Robbins - Word MVP on news.microsoft.com

Hey! Come back here and work out how to fix macropod's macro method.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Gordon Bentley-Mix on news.microsoft.com"
 
M

macropod

Hi Doug,

That's odd. For some reason 'Weekday * (EndDate Mod 7 <> Weekday)' doesn't evaluate correctly when the EndDate day is a Saturday.
Still, this can be overcome by changing:
EndDate = Int(EndDate / 7) * 7 - Weekday * (EndDate Mod 7 <> Weekday)
to:
If EndDate Mod 7 <> Weekday Then EndDate = Int(EndDate / 7) * 7 + Weekday

--
Cheers
macropod
[MVP - Microsoft Word]


Doug Robbins - Word MVP on news.microsoft.com said:
Hi Paul,

Not quite. It does not correctly handle when Date is a Saturday.

I haven't yet figured out what would need to be done to it to correct that.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

macropod said:
Hi Doug,

How about:
Sub GetDate()
Dim Days As Integer, Weekday As Integer, EndDate As Date
Days = 90
Weekday = 6
EndDate = DateAdd("d", Days, Date)
EndDate = Int(EndDate / 7) * 7 - Weekday * (EndDate Mod 7 <> Weekday)
EndDate = EndDate - ((Format(EndDate, "d") < 7) + (Format(EndDate, "d") > 28) * 2 + _
(Format(EndDate, "d") > 14) * (Format(EndDate, "d") < 22)) * 7
MsgBox "The Date is " & Format(EndDate, "dddd, d MMM yyyy")
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Doug Robbins - Word MVP on news.microsoft.com said:
How about the following in which the For j ... Next is just for testing

Dim today As Date, today1 As Date
Dim i As Long
Dim j As Long
today = Date

For j = 0 To 15
today1 = DateAdd("d", j, today)
Select Case Format(today1, "ddd")
Case "Sat"
i = 97
Case "Sun"
i = 96
Case "Mon"
i = 95
Case "Tue"
i = 94
Case "Wed"
i = 93
Case "Thu"
i = 92
Case Else
i = 91
End Select
Select Case Format(DateAdd("d", i, today1), "d")
Case Is < 8
i = i + 7
Case 15 To 20
i = i + 7
End Select
MsgBox "The Date is " & Format(DateAdd("d", i, today1), "dddd d MMMM yyyy")
Next j


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Doug,

I think it is more involved than that. I think the OP set the condition that the future date be a second or fourth Friday of
the future month. This isn't fully tested, but works for the limited testing that I did:

Sub ScratchMacro()
Dim i As Long
Dim bConMet As Boolean
Dim myDate As Date
Dim anchorDate As Date
Dim today As String
Dim lngSecond As Long
Dim lngFourth As Long
bConMet = False
myDate = DateSerial(2009, 4, 6) '(April 6 2009 used for testing)
myDate = DateAdd("d", 90, myDate)
Do
anchorDate = DateSerial(Year(myDate), Month(myDate), 1)
Select Case Format(anchorDate, "ddd")
Case "Fri"
lngSecond = 8
lngFourth = 22
Case "Sat"
lngSecond = 14
lngFourth = 28
Case "Sun"
lngSecond = 13
lngFourth = 27
Case "Mon"
lngSecond = 12
lngFourth = 26
Case "Tue"
lngSecond = 11
lngFourth = 25
Case "Wed"
lngSecond = 10
lngFourth = 24
Case "Thur"
lngSecond = 9
lngFourth = 23
End Select
Select Case Day(myDate)
Case Is > lngFourth
myDate = Format(DateAdd("d", 1, myDate), "d MMMM yyyy")
Case Is <= lngSecond
MsgBox DateSerial(Year(myDate), Month(myDate), lngSecond)
bConMet = True
Case Is <= lngFourth
MsgBox DateSerial(Year(myDate), Month(myDate), lngFourth)
bConMet = True
End Select
Loop Until bConMet
End Sub

Doug Robbins - Word MVP on news.microsoft.com wrote:
Use

Dim today As String
Dim i As Long

today = Format(my_date, "ddd")
Select Case today
Case "Sat"
i = 104
Case "Sun"
i = 103
Case "Mon"
i = 102
Case "Tue"
i = 101
Case "Wed"
i = 100
Case Thur
i = 99
Case Else
i = 98
End Select
MsgBox "The Date is " & Format(DateAdd("d", i, my_date), "dddd d MMMM
yyyy")

I'm trying to see if I can take a date from a custom field "my_date"
and calculate from that the soonest 2nd or 4th Friday that is at
least 90 days after "my_date".

I've seen a few examples of some hefty / sophisticated ways to
manipulate dates in Office / Word...but nothing that leads me to
understand it enough to
do what I'm describing above.
 
H

Hamoth

I see. I was just pasting it in.

The key commands on a Mac would be different, right?
Is there a menu option I can use to insert these brace pairs?
 

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