Count Number Of Days Except Sunday

mgirvin

Well-known Member
Joined
Dec 15, 2005
Messages
1,245
Office Version
  1. 365
Platform
  1. Windows
Dear Smartest Excel People In The World,

In B13 I have the start date: 12/1/2008
In C13 I have the end date: 1/5/2009
In cell D13 I want a formula that counts the number of days between the two dates that are not Sunday. The start and end dates are included in the count.

I have created this formula using Ctrl + Shift + Enter:

{=SUM(IF(WEEKDAY(ROW(INDIRECT(B13&":"&C13)),2)<>7,1,0))}

I have also created this formula using Ctrl + Shift + Enter:

{=SUM(IF(TEXT(ROW(INDIRECT(B13&":"&C13)),"ddd")<>"Sun",1,0))}

They both seem to work. I get a result of 31.

Is there a formula that is better than this, more efficient than this, or "less expensive"?
 
Dear houdini,

Now that is an efficient and cleaver formula! Can you explain the logic of it to me?

Thanks!
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Do you plan, Mike, to evaluate which formula is fastest? It would be interesting to know.

Dear Fazza,

Sure, but the unit of time I know is "Enter" and "Ctrl + Shift = Enter". So they each tie. No, no! I don't know how to measure which is the fastest. Do you know how?
 
Upvote 0
Hello Mike,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
To explain it’s probably better to do so using a slightly different formula. This formula will calculate the number of Sundays between 2 dates, A2 and B2<o:p></o:p>
<o:p> </o:p>
=INT((WEEKDAY(A2-1)+B2-A2)/7)<o:p></o:p>
<o:p> </o:p>
The explanation is fairly intuitive<o:p></o:p>
<o:p></o:p>
If you have any time period 6 days long, e.g. Monday to Sunday (including 7 different days) then how many Sundays are there in that period (assuming we count both start and end dates)? The answer is 1. Similarly any 13 day period has 2 Sundays....etc.<o:p></o:p>
<o:p></o:p>
.....but what if the period is 7 days long, how many Sundays will there be? The answer is 1.....unless the start date is a Sunday, in which case the answer is 2...or what about 8 days......again the answer is 1....unless the start date is a Saturday or Sunday.<o:p></o:p>
<o:p></o:p>
So it's sufficient to know 2 things, the length of the period and the weekday of the start date.<o:p></o:p>
<o:p></o:p>
Given the above logic if we calculate the length of the period and add 7 if the start date is a Sunday, add 6 if the start date is a Saturday, add 5 if the start date is a Friday etc….then divide the result by 7 and round down to the nearest integer, we’ll get the number of Sundays<o:p></o:p>
<o:p> </o:p>
So B2-A2 gives the length of the period and WEEKDAY(A2-1) adds 7 for Sunday, 6 for Saturday etc. then you divide the whole amount by 7 and INT rounds down.<o:p></o:p>
<o:p> </o:p>
You can adapt that formula to count any day of the week, change the 1 to a 2 to count Mondays, 3 for Tuesdays, 4 for Wednesdays etc.<o:p></o:p>
<o:p> </o:p>
Of course you wanted to count days that aren’t Sundays so to do that you can subtract the above from the total number of days, i.e.<o:p></o:p>
<o:p> </o:p>
=B2-A2+1-INT((WEEKDAY(A2-1)+B2-A2)/7)<o:p></o:p>
<o:p> </o:p>
The formula I posted originally uses the same concepts but examines the weekday of the end date, so you end up with a slightly different formula, i.e.<o:p></o:p>
<o:p> </o:p>
=B2-A2-INT((1+B2-A2-WEEKDAY(B2))/7)<o:p></o:p>
<o:p> </o:p>
Both of these will give exactly the same result. The second one derives from a slightly different way to count Sundays between 2 dates, i.e.<o:p></o:p>
<o:p> </o:p>
=INT((8-WEEKDAY(B2)+B2-A2)/7)<o:p></o:p>
<o:p> </o:p>
This formula only references the start date once, so it’s useful to use when the start date has to be derived from a calculation, you only have to include that calculation once.<o:p></o:p>
<o:p> </o:p>
Note: one more way to count the non–Sundays, using a similar approach<o:p></o:p>
<o:p> </o:p>
=SUM(INT((WEEKDAY(A2-{2,3,4,5,6,7})+B2-A2)/7))<o:p></o:p>
<o:p> </o:p>
In this formula the “array constant” {2,3,4,5,6,7} represents <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:City w:st="on"><st1:place w:st="on">Mons</st1:place></st1:City>, Tues, Weds, Thurs, Fris, Sats. This version is useful because you can adapt it to count any combination of days.<o:p></o:p>
<o:p> </o:p>
You want to count Mondays and Fridays in a period?<o:p></o:p>
<o:p> </o:p>
=SUM(INT((WEEKDAY(A2-{2,6})+B2-A2)/7))<o:p></o:p>
<o:p> </o:p>
Regards, barry<o:p></o:p>
 
Upvote 0
Thanks, Barry. My formula was wrong & your formula is much smarter. Regards, Fazza

Mike, I guess for timing the formula's speed you could load up a worksheet with 60,000 formulas and have some VBA do something like
Code:
set calculation mode to manual
start timer
run a loop say 100 times
force a worksheet calculation
end loop
stop timer
For the timer, I think there is an API approach that is best, not just simply using something like dtMyTimer = Now. A quick google should fine that.

cheers, Fazza
 
Upvote 0
Dear houdini,

Wow!

Your description is so well done! It is thorough and easy to follow.

And plus the extra knowledge for other related situations.

Thanks a million!
 
Upvote 0
Dear Fazza,

Thanks! I will try a quick google!
 
Upvote 0
Dear houdini,

Opps!

I thought I got it, but I did not understand this part:

"If you have any time period 6 days long, e.g. Monday to Sunday (including 7 different days)"

When I count Monday to Sunday, I count 7.

What am I missing?
 
Upvote 0
Well, it's 6 days long if you calculate enddate-startdate, e.g. yesterday was Monday 5th, Sunday is the 11th so

=11-5 = 6

.....but obviously if you count the days, including start date and end date, there are 7 :)

It's an old argument really......but doesn't make much difference to the other calculations, they're still valid either way
 
Upvote 0
Well, it's 6 days long if you calculate enddate-startdate, e.g. yesterday was Monday 5th, Sunday is the 11th so

=11-5 = 6

.....but obviously if you count the days, including start date and end date, there are 7 :)

It's an old argument really......but doesn't make much difference to the other calculations, they're still valid either way

True, old argument that I usually see you in providing solutions for allot of the times on this forum :)
 
Upvote 0
Dear houdini,

Whew! I am glad that it is that simple.

Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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