Weeks in a given month

engineer69

New Member
Joined
Apr 3, 2009
Messages
34
I have been unable to get VBA (not Excel formulas) on how to find the amount of weeks in a given month. Example: for the date of 1/1/2018 there are 5 weeks and for 2/1/2018 there are 4 weeks.

This Excel formula seems to work "=4+(DAY(A21-DAY(A21)+35)< WEEKDAY(A21-DAY(A21)-3))", but I need it in VBA. Has anybody found a way to do this?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Code:
Sheets("Sheet1").Range("A1").Formula = "=4+(DAY(A21-DAY(A21)+35)< WEEKDAY(A21-DAY(A21)-3))"
 
Upvote 0
Code:
Sub test()
Dim myVar As Long
Dim refCell As Date

refCell = Sheets("Sheet1").Range("A21").Value

myVar = 4 + ((Day(refCell - Day(refCell) + 35)) < Weekday(refCell - Day(refCell) - 3))

MsgBox myVar

End Sub
 
Upvote 0
Now this is getting weird. If I use the formula in a cell 1/1/2018 returns 5 weeks, which is correct. If I use a variable I get 3 weeks???
 
Upvote 0
If you want to use it in your VBA code without referencing to your sheet, this is a UDF

Code:
Sub test()

Dim Wks As Long
Wks = No_Of_Weeks(#1/12/2017#)

Debug.Print Wks

End Sub

Function No_Of_Weeks(dDate As Date) As Byte

No_Of_Weeks = IIf((Day(dDate - Day(dDate) + 35)) < WorksheetFunction.Weekday(dDate - Day(dDate) - 3), 5, 4)

End Function
 
Upvote 0
The formula in my code was incorrect. Also, it doesn't have to be a UDF, but makes it easier to use in your Sheet(s) if you so desire.
 
Upvote 0
As bs0d said, it doesn’t have to be a UDF to work but I thought this is what you were after to use in VBA code. You can still use the same line below in any code without a UDF & should get the same results

Code:
‘ Just define the dDate & assing a value to it
No_Of_Weeks = IIf((Day(dDate - Day(dDate) + 35)) < WorksheetFunction.Weekday(dDate - Day(dDate) - 3), 5, 4)

Glad to help :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,716
Members
452,995
Latest member
isldboy

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