Weeks

mort1703

Active Member
Joined
Sep 13, 2008
Messages
266
Hi

I need to create a macro which allows me to enter a month and year.

Using this information I need to work out how many weeks are in the month, using Monday as the first day of the week,

ie.
In April 2009 there are 4 Mondays
In June 2009 there are 5 Mondays

Any suggestions?
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Does it need to be a macro, you can do this easily with a formula......put the first of the relevant month in A1 then this formula in B1 gives the Mondays in that month

=INT((WEEKDAY(A1-2)+31-DAY(A1+31))/7)
 
Upvote 0
Hi, There's probably a better way with a bit more thought, but its a start.
Code:
Sub Monday()
Dim Dt As String, fdate As Date, Yr As Integer, Mth As String
Dim Mon As Date, c As Integer
On Error Resume Next
 Dt = Application.InputBox(prompt:="Enter Date as E.g. :- April/2009 ", Title:="Number of Mondays", Type:=2)
 If Dt = "" Then Exit Sub
    Mth = Split(Dt, "/")(0)
        Yr = Split(Dt, "/")(1)
            fdate = "1" & " / " & Mth & " / " & Yr

    For Mon = fdate To DateAdd("m", 1, fdate)
        If WeekdayName(Weekday(Mon, vbMonday)) = "Monday" Then
            c = c + 1
            End If
    Next Mon
MsgBox "There Were " & c & " Mondays in " & Dt

End Sub
Regards Mick
 
Upvote 0
Hi Mick

I think there is an issue with the code, I get 5 Mondays in MAY/2009 when I should have 4.

I had a deeper look at the code, and the issue seems to be with the FOR LOOP, on the last day of the month it runs through and includes the 1st of the next month as a result it will give an incorrect answer when the last day of the month occurs on SUNDAY, I'm not sure how I modify to stop this happening, If I can stop this I think the code will do the job?
 
Upvote 0
Hi, Slight Oversight !!
Try:-
Code:
Sub Monday()
Dim Dt As String, fdate As Date, Yr As Integer, Mth As String
Dim Mon As Date, c As Integer
On Error Resume Next
 Dt = Application.InputBox(prompt:="Enter Date as e.g. :- April/2009 ", Title:="Number of Mondays", Type:=2)
 If Dt = "" Then Exit Sub
    Mth = Split(Dt, "/")(0)
        Yr = Split(Dt, "/")(1)
            fdate = "1" & " / " & Mth & " / " & Yr

    
    For Mon = fdate To (DateAdd("m", 1, fdate) - 1)
        If WeekdayName(Weekday(Mon, vbMonday)) = "Monday" Then
            c = c + 1
            End If
     Next Mon

MsgBox "There were " & c & " Mondays in " & Dt

End Sub
Regards Mick
 
Upvote 0
Not well tested, but I believe fairly accurately contrived from Barry's, but VBA:

Code:
Sub exCall()
    MsgBox Mondays_Ret(#6/1/2009#)
End Sub
 
Function Mondays_Ret(MyDate As Date) As Long
    Mondays_Ret = Int((Weekday(MyDate - 2, vbSunday) + 31 - Day(MyDate + 31)) / 7)
End Function

If it works, it is Mr. Houdini's, if not, my botch...

Mark
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,378
Members
448,955
Latest member
BatCoder

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top