helpimanewb
New Member
- Joined
- Mar 14, 2014
- Messages
- 4
Trying to write my first user defined function, but not having much success. Have a light programming background (mostly Python).
The function takes a date and returns the most appropriate meeting date from a list of all the first Fridays of the month during the year. I'm not sure exactly where I went wrong. Code for the two functions used is listed below.
Thank you for your help in advance!
The function takes a date and returns the most appropriate meeting date from a list of all the first Fridays of the month during the year. I'm not sure exactly where I went wrong. Code for the two functions used is listed below.
Thank you for your help in advance!
Code:
Public Function TeamDecisionDate(dateExpiration As Date) As Date
'Function takes expiration date of contract and returns best date for team to decide on action.
'Define local variables
Dim dateAnalytics As Date
Dim dateIP As Date
'Dim dateSP As Date
'Dim arraystringTeamSchedule() As String
Dim arraydatesTeamSchedule(11) As Date
Dim dateTeamDecision As Date
Dim y As Variant
'Analytics to begin 161 days before
dateAnalytics = dateExpiration - 161
dateIP = dateAnalytics + 5
'dateSP = dateExpiration - 5
'Assigns date of every first Friday of the month for the year 2014 to arraydatesTeamSchedule -- utilizes dhNthWeekday function provided by MSDN
For x = 1 To 12
arraydatesTeamSchedule(x - 1) = dhNthWeekday(DateSerial(2014, x, 1), 1, vbFriday)
Next x
'Assigns initial value of dateTeamDecision to first Friday of the year (first value in team meeting date array).
dateTeamDecision = arraydatesTeamSchedule(0)
'Evaluates each team meeting date against current value of dateTeamDecision to find the value nearest to exactly 60 days from dateIP
For Each y In arraydatesTeamSchedule
If Abs(dateIP - y - 60) < Abs(dateIP - dateTeamDecision - 60) Then
dateTeamDecision = y
End If
Next y
'Assigns best team decision date to function variable for return
TeamDecisionDate = dateTeamDecision
End Function
Code:
Public Function dhNthWeekday(dtmDate As Date, intN As Integer, _
intDOW As Integer) As Date
' Find the date of the specified day within the month. For
' example, retrieve the 3rd Tuesday's date.
Dim dtmTemp As Date
If (intDOW < vbSunday Or intDOW > vbSaturday) _
Or (intN < 1) Then
' Invalid parameter values. Just
' return the passed-in date.
dhNthWeekday = dtmDate
Exit Function
End If
' Get the first of the month.
dtmTemp = DateSerial(Year(dtmDate), Month(dtmDate), 1)
' Get to the first intDOW in the month.
Do While Weekday(dtmTemp) <> intDOW
dtmTemp = dtmTemp + 1
Loop
' Now you've found the first intDOW in the month.
' Just add 7 for each intN after that.
dhNthWeekday = dtmTemp + ((intN - 1) * 7)
End Function