# Weeks

#### mort1703

##### Active Member
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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

#### barry houdini

##### MrExcel MVP
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)

#### MickG

##### MrExcel MVP
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

#### mort1703

##### Active Member
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?

#### MickG

##### MrExcel MVP
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

#### GTO

##### MrExcel MVP
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

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

1,164,681
Messages
5,838,774
Members
430,568
Latest member
bortey

### 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.

### Which adblocker are you using?

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

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