Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find number of Tuesdays in a given Month 2

Status
Not open for further replies.

VickyC

Technical User
Sep 25, 2010
206
CA
hello to all

I am struggling to write a function that does the following...

Function NumOfWDinMonth(datDateIn As Date, intDayOfWeek As Integer) As Integer

If I input datDateIn = #September 25, 2010# and intDayOfWeek = 3, the output should be 4 (because there are 4 Tuesdays in September, 2010).

Thanks in advance for any help.
VickyC
 
Try something like:
Code:
Public Function NumWDInMonth(datDateIn As Date, intDayOfWeek As Integer) As Integer
    Dim datSOM As Date  'date of start of month
    datSOM = DateAdd("d", -Day(datDateIn) + 1, datDateIn)
    Do While Month(datSOM) = Month(datDateIn)
        If Weekday(datSOM) = intDayOfWeek Then
            NumWDInMonth = NumWDInMonth + 1
        End If
        datSOM = datSOM + 1
    Loop
End Function

Duane
Hook'D on Access
MS Access MVP
 
How are ya VickyC . . .

Another way ... simply supply any date in the month:
Code:
[blue]Public Function NTues(usrDate As Date) As Long
   Dim begDate As Date, endDate As Date
   
   endDate = DateSerial(Year(usrDate), Month(usrDate) + 1, 0)
   begDate = endDate - Day(endDate) + 1
   NTues = DateDiff("ww", begDate, endDate, vbTuesday) - Int(vbTuesday = Weekday(begDate))
End Function[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top