Welcome toExcel.Tips.Net
Tips.Net Home
ExcelTips Home
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Learn Access Now
Free Printable Forms
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site
Adding a Little Animation to Your Life
Converting a Range of URLs to Hyperlinks
Making the Formula Bar Persistent
Ever wonder how many of a particular weekday occur within a given month? For some people, it is important to know how many Tuesdays there are in a month. And who doesn't want to know whether a particular month will have four or five Saturdays?
Excel does not include an intrinsic function that you can use to determine the number of times a particular weekday occurs within a given month. You can, however, create your own formulas and functions to accomplish the task.
First, consider the following formula.
=4+N((WEEKDAY(DATE(YEAR($A$1),MONTH($A$1),1)))+(DAY(DATE(YEAR($A$1),MONTH($A$1)+1,0))-28)>(7*((WEEKDAY(DATE(YEAR($A$1),MONTH($A$1),1)))>(1+ROW()-ROW($A$2)))+(1+ROW()-ROW($A$2))))
The formula relies on a date in A1. This date should be from the month you want "tested." The formula is meant to be copied into a cell in row 2, and then copied to the six cells directly beneath that. For instance, you could copy this formula to the range of cells B2:B8. The first response (B2) is the number of Sundays in the month, the second (B3) is the number of Mondays, and so on.
The drawback to this formula is that it uses the position of the cell containing the formula as part of the formula. This means that the formula must be placed somewhere beginning in the second row.
Another drawback is that the formula is quite long and complex. If you want a shorter formula, then you need to turn to an array formula. One handy formula you can use assumes that you provide three arguments: the year (cell C2), the month (cell D2), and a weekday (cell E2). With these three items, the following formula works great:
=SUM(IF(WEEKDAY(DATE(C2,D2,ROW(INDIRECT("1:"&DAY(DATE(C2,D2+1,0))))))=E2,1,0))
Remember that this is an array formula, which means that you must enter it by pressing Shift+Ctrl+Enter. In addition, the weekday value you enter in cell E2 must be in the range of 1 through 7, where 1 is Sunday, 2 is Monday, etc.
If your worksheet design doesn't allow for you to enter the year, month, and weekday in different cells, a clean solution is to create a user-defined function to return the count. The following macro is an example of this type of function.
Function MonthWeekDays(dDate As Date, iWeekDay As Integer)
Dim dLoop As Date
If iWeekDay < 1 Or iWeekDay > 7 Then
MonthWeekDays = CVErr(xlErrNum)
Exit Function
End If
MonthWeekDays = 0
dLoop = DateSerial(Year(dDate), Month(dDate), 1)
Do While Month(dLoop) = Month(dDate)
If WeekDay(dLoop) = iWeekDay Then _
MonthWeekDays = MonthWeekDays + 1
dLoop = dLoop + 1
Loop
End Function
You use the function by entering the following in a cell:
=MonthWeekDays(A1,4)
In this usage, the first argument (cell A1) contains a date in the month being evaluated. The second argument is a numeric value representing the weekday that you want to count. This value must be in the range of 1 to 7, where 1 is Sunday, 2 is Monday, and so on.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2183) applies to Microsoft Excel versions: 97 2000 2002 2003
More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want.