Home All Groups Group Topic Archive Search About

How to find 3rd tuesday of the month

Author
16 Mar 2005 5:35 PM
Tyler
I have a list of codes that correspond to a certain day of the month.  I have
to be able to find what date the 1st Monday is, or the 2nd Tuesday is, etc....

Is there a function that will help me out.  Any suggestions at all?

Thanks
--
Tyler

Author
1 Jun 2005 11:18 PM
Taylor
"Tyler" wrote:

> I have a list of codes that correspond to a certain day of the month.  I have
> to be able to find what date the 1st Monday is, or the 2nd Tuesday is, etc....
>
> Is there a function that will help me out.  Any suggestions at all?
>
> Thanks
> --
> Tyler

Poor Guy Asked how to find the 3rd Tuesday of Every Month and you guys go on
about Ricks extreme code and that and all i have seen it a lot of examples of
the whole year and or months ect...

The Following can be called anywhere
I used a blank form with a multilined Text Box and a Command Button

Private Sub Command1_Click()

Dim I

Call Get_3rd_Tues_Of_Mnth

For I = 1 To 12
'// i put this just to show the results
Text1.Text = Text1.Text & DateList(I) & vbCrLf
Next I

End Sub

**************************
Next put the following in a Module
**************************

Option Explicit

Public DateList(12) As String
Public D_Count

Public Function Get_3rd_Tues_Of_Mnth() As Integer
Dim MT
Dim DT As Date
Dim YTD
Dim ReveDT As Date
Dim RevMT As Integer
Dim I
Dim Query
Dim DateSearch As Date
Dim TuesCount As Integer

'// Erase ArrayList
Erase DateList
'// Set to Zero
D_Count = 0

For MT = 1 To 12

DT = MT & "/01/" & Year(Date$)
'// Since Starting with 1 Already Only Use 1 to 31 Which Will Cover 31 Days
For I = 1 To 31
'// Since Dim RevDT As Date We Can Add +1 This Will Advance Day By Day on
Each Loop
    ReveDT = DT + I
'// After Advancing Day By Day Get Number Of Month
    RevMT = Month(ReveDT)
'// If MT = 12 AKA December then we will set Days of Month to 31
        If MT = 12 Then
            Get_3rd_Tues_Of_Mnth = 31
        Else
'// If Starting Month Is Less The Month Calculated Then (i) will Be Our Days
Of Month
            If MT < RevMT Then
'// Set How Many Days In Month For Later Use
                Get_3rd_Tues_Of_Mnth = I
               GoTo DTSearch:
            End If

        End If
Next I

DTSearch:

'// Use Days of Month as your loop indicator
For Query = 1 To Get_3rd_Tues_Of_Mnth
'// on each loop Set Date
    DateSearch = MT & "/" & Query & "/" & Year(Date$)
'// Compare Date to WeekDay Number Which we are looking for 3
        If Weekday(DateSearch) = 3 Then
'// If Weekday Number 3 is found add 1 to count
            TuesCount = TuesCount + 1
'// When third Tuesday is Found then Send to Text box or An Array
                If TuesCount = 3 Then
                    D_Count = D_Count + 1
'// You Can Buld this into an Array and make better use of this
                    DateList(D_Count) = DateSearch
'// Reset Tuesday Count to search Next Month
                    TuesCount = 0
                    GoTo NextMonth:
                End If
        End If
'// Search Next Day Of The Week
Next Query

NextMonth:

Next MT

End Function


Hope this Helps i use a lot of this in a Vacation Tracker for Work
Author
2 Jun 2005 12:39 AM
MikeD
Show quote Hide quote
"Taylor" <Tay***@discussions.microsoft.com> wrote in message
news:309DA5EC-EEA5-4E33-82B9-322DBE65511A@microsoft.com...
>
>
> "Tyler" wrote:
>
>> I have a list of codes that correspond to a certain day of the month.  I
>> have
>> to be able to find what date the 1st Monday is, or the 2nd Tuesday is,
>> etc....
>>
>> Is there a function that will help me out.  Any suggestions at all?
>>
>> Thanks
>> --
>> Tyler
>
> Poor Guy Asked how to find the 3rd Tuesday of Every Month and you guys go
> on
> about Ricks extreme code and that and all i have seen it a lot of examples
> of
> the whole year and or months ect...
>
> The Following can be called anywhere
> I used a blank form with a multilined Text Box and a Command Button
>
> Private Sub Command1_Click()
>
> Dim I
>
> Call Get_3rd_Tues_Of_Mnth
>
> For I = 1 To 12
> '// i put this just to show the results
> Text1.Text = Text1.Text & DateList(I) & vbCrLf
> Next I
>
> End Sub
>
> **************************
> Next put the following in a Module
> **************************
>
> Option Explicit
>
> Public DateList(12) As String
> Public D_Count
>
> Public Function Get_3rd_Tues_Of_Mnth() As Integer
> Dim MT
> Dim DT As Date
> Dim YTD
> Dim ReveDT As Date
> Dim RevMT As Integer
> Dim I
> Dim Query
> Dim DateSearch As Date
> Dim TuesCount As Integer
>
> '// Erase ArrayList
> Erase DateList
> '// Set to Zero
> D_Count = 0
>
> For MT = 1 To 12
>
> DT = MT & "/01/" & Year(Date$)
> '// Since Starting with 1 Already Only Use 1 to 31 Which Will Cover 31
> Days
> For I = 1 To 31
> '// Since Dim RevDT As Date We Can Add +1 This Will Advance Day By Day on
> Each Loop
>    ReveDT = DT + I
> '// After Advancing Day By Day Get Number Of Month
>    RevMT = Month(ReveDT)
> '// If MT = 12 AKA December then we will set Days of Month to 31
>        If MT = 12 Then
>            Get_3rd_Tues_Of_Mnth = 31
>        Else
> '// If Starting Month Is Less The Month Calculated Then (i) will Be Our
> Days
> Of Month
>            If MT < RevMT Then
> '// Set How Many Days In Month For Later Use
>                Get_3rd_Tues_Of_Mnth = I
>               GoTo DTSearch:
>            End If
>
>        End If
> Next I
>
> DTSearch:
>
> '// Use Days of Month as your loop indicator
> For Query = 1 To Get_3rd_Tues_Of_Mnth
> '// on each loop Set Date
>    DateSearch = MT & "/" & Query & "/" & Year(Date$)
> '// Compare Date to WeekDay Number Which we are looking for 3
>        If Weekday(DateSearch) = 3 Then
> '// If Weekday Number 3 is found add 1 to count
>            TuesCount = TuesCount + 1
> '// When third Tuesday is Found then Send to Text box or An Array
>                If TuesCount = 3 Then
>                    D_Count = D_Count + 1
> '// You Can Buld this into an Array and make better use of this
>                    DateList(D_Count) = DateSearch
> '// Reset Tuesday Count to search Next Month
>                    TuesCount = 0
>                    GoTo NextMonth:
>                End If
>        End If
> '// Search Next Day Of The Week
> Next Query
>
> NextMonth:
>
> Next MT
>
> End Function
>
>
> Hope this Helps i use a lot of this in a Vacation Tracker for Work
>

First, do you realize the post you replied to, as well as all previous
replies, were nearly 3 months ago?  The OP's probably not checking back for
additional replies anymore.

Second, IMO, that is NOT good code.  You've got variables of type Variant
for no reason.  You've got variable names that are meaningless or unclear.
You're using GoTo to jump to different parts of the function.  You're using
the function name as the end value of a For loop (and while in VB a
function's name is essentially a variable within the function's scope, it's
a very bad practice IMO).  And that's all without even copying and pasting
your code into VB and actually trying/testing it.  While I don't offhand
know if this could be problematic or not, just the fact that you're looping
through 31 (days) is troublesome to me since not all months have 31 days.

I don't mean to insult or offend you, but that code needs cleaned up
considerably.  You mentioned that code was for an application where you
work.  Personally, if you worked under me and wrote code like that, I'd tell
you to re-write it (offering whatever help I could provide).  That's just
nasty code.  The underscores in the function name are abhorrent.  Not to
mention are you really going to write a function like that for each day of
each week (Get_3rd_Mon, Get_2nd_Tues, Get_4th_Fri)?.  The function needs to
be more generic.  Pass in as arguments the day and week for the date you
need returned.

Like I said, I'm not trying to insult you or your code so don't take offense
to my criticism.  I'm just trying to point out some things that I personally
think are bad about it. Maybe it'll encourage you to make some code changes
so that your programs and your code will be better.


--
Mike
Microsoft MVP Visual Basic
Author
2 Jun 2005 9:41 PM
Taylor
yes it was nearly 3 months ago but also i do not see Tyler coming back to say
thanks or anything and probably still gets notified of posting via email.
your not offending me at all i kind of expected that and was looking forward
to someone digging into it and ripping it apart as it seems to be a common
thing on this community thats why i did it. =c)

This code was from what i wrote to pull every day of the year for a program
that i use at work. this is not exact code but a quick snip from here and
there.

i added the fact to look for the 3rd tuesday of EVERY month.

    "While I don't offhand know if this could be problematic or not,
    just the fact that you're looping through 31 (days) is troublesome
    to me since not all months have 31 days."

Yes it does look as if it loops 31 time for days but if you look closer
it will only go till the month end and then advance to the next month to get
those days ect. So it can loop to 31 days if the month allows it but it will
only loop up to 31 times if needed.

i normally use this code to build out a calendar for everyday of the
month/year.

    "The underscores in the function name are abhorrent.  Not to
    mention are you really going to write a function like that for each day of
    each week (Get_3rd_Mon, Get_2nd_Tues, Get_4th_Fri)?. The function
                needs to be more generic.  Pass in as arguments the day and
week for
                the date you need returned."

The Function name is the Name of the Subject thats why it is like that.
and no im not going to write a function for each day of each week.
this function pulls every 3rd tuesday for the entier year and stores it in
an Array for later use.

       "And that's all without even copying and pasting your code into VB
and actually
        trying/testing it."

one thing that gets me is you said yourself you didnt even copy&paste to try
the code out to see if it really worked, you just looked at the code and
could see.
Yes the codes Choppy and probably unclear or as in your words meaningless.
But just by looking at something you dont always get the full
function/reality of what it is or does till you try it.

Thanks for the input and i will put thought to changing my code and or
getting more advise from you since you seem to be the only one trying to give
me an out look of what im really doing or trying to point me in the right
direction.

Thanks Again.

Taylor~


Show quoteHide quote
> First, do you realize the post you replied to, as well as all previous
> replies, were nearly 3 months ago?  The OP's probably not checking back for
> additional replies anymore.
>
> Second, IMO, that is NOT good code.  You've got variables of type Variant
> for no reason.  You've got variable names that are meaningless or unclear.
> You're using GoTo to jump to different parts of the function.  You're using
> the function name as the end value of a For loop (and while in VB a
> function's name is essentially a variable within the function's scope, it's
> a very bad practice IMO).  And that's all without even copying and pasting
> your code into VB and actually trying/testing it.  While I don't offhand
> know if this could be problematic or not, just the fact that you're looping
> through 31 (days) is troublesome to me since not all months have 31 days.
>
> I don't mean to insult or offend you, but that code needs cleaned up
> considerably.  You mentioned that code was for an application where you
> work.  Personally, if you worked under me and wrote code like that, I'd tell
> you to re-write it (offering whatever help I could provide).  That's just
> nasty code.  The underscores in the function name are abhorrent.  Not to
> mention are you really going to write a function like that for each day of
> each week (Get_3rd_Mon, Get_2nd_Tues, Get_4th_Fri)?.  The function needs to
> be more generic.  Pass in as arguments the day and week for the date you
> need returned.
>
> Like I said, I'm not trying to insult you or your code so don't take offense
> to my criticism.  I'm just trying to point out some things that I personally
> think are bad about it. Maybe it'll encourage you to make some code changes
> so that your programs and your code will be better.
>
>
> --
> Mike
> Microsoft MVP Visual Basic