Count number of days within certain month within a date range

Marmasek

New Member
Joined
Nov 20, 2019
Messages
8
I am looking for assistance to create a formula that counts the number of days within a certain month within a date range. The numbers found in C2, D2, E2 and F2 are where I would like to input a formula to auto populate after entering the start and end dates.

For example:
Start DateEnd DateJanFebMarApr
30 Jan 20205 Apr 2020229305
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
March has 31 days, doesn't it?

Book1
ABCDEF
1Start DateEnd DateJanFebMarApr
21/30/20204/5/2020229315
Sheet1
Cell Formulas
RangeFormula
C2C2=EOMONTH((A2),0)-A2+1
D2D2=DAYS(EOMONTH(A2,1),EOMONTH(A2,0))
E2E2=DAYS(EOMONTH(A2,2),EOMONTH(A2,1))
F2F2=B2-(EOMONTH(B2,-1))
 
Upvote 0
Looking at your response, I'm not sure if I made myself clear. I'll try explaining further:

If input the start dates and end dates manually, I would like a table with all the months in the columns to autofill how many days in each month have been covered.

For example if I entered the following data into the table:

Start dateEnd dateJanFebMarAprMayJunJulAugSepOctNovDec
30 Jan 20205 Apr 2020
4 May 20203 Jun 2020

I would like the result in from C2:N3 to auto populate to:

Start dateEnd dateJanFebMarAprMayJunJulAugSepOctNovDec
30 Jan 20205 Apr 2020229315
4 May 20203 Jun 2020263
 
Upvote 0
or better yet:

Start dateEnd dateJanFebMarAprMayJunJulAugSepOctNovDec
30 Jan 20205 Apr 202022931500000000
4 May 20203 Jun 20200000263000000
 
Upvote 0
If 30 Jan 2020 is 2 days on January then why isn't 4 May 2020 28 days of May?
 
Upvote 0
I'll assume the 4 May 2020 entry should show 28 days remaining in May.

I need the year of the heading month so it doesn't count 2019 or 2021 dates so I've entered the actual 1st of the month for each column and just formatted it a mmm-yy.

1574280309429.png


Book1
ABCDEFGHIJKLMN
1Start dateEnd date1/1/20202/1/20203/1/20204/1/20205/1/20206/1/20207/1/20208/1/20209/1/202010/1/202011/1/202012/1/2020
21/30/20204/5/202022931500000000
35/4/20206/3/20200000283000000
Sheet1
Cell Formulas
RangeFormula
D1:N1D1=EOMONTH(C1,0)+1
C2:N3C2=IF(EOMONTH($A2,0)=EOMONTH(C$1,0),EOMONTH($A2,0)-$A2+1,IF(EOMONTH($B2,0)=EOMONTH(C$1,0),$B2-EOMONTH($B2,-1),IF(AND(C$1>$A2,C$1<$B2),EOMONTH(C$1,0)-EOMONTH(C$1,-1),0)))
 
Upvote 0
Thank you Toadstool. I wish I had skills to create formulas rather than just copy paste and try and decipher how you derived them!
 
Upvote 0
You're welcome!

I'm sure you could figure it out. It's just practice and the list of available functions:
Excel functions (alphabetical)

Excel holds dates as an integer since 1 Jan 1900. If I take today's date and add 1 I'll get tomorrow's date.

EOMONTH (end of month) has two parameters, a start date and a number of months to offset. It gives the end of the specified month (e.g. =EOMONTH(TODAY(),-1)+1 takes the current date, gives the last day of the previous month (31-Oct-2019) and the +1 is plus 1 day so the result is 1-Nov-2019)

IF has three parameters, IF(condition, result if true, result if false).

Your cells for counting days have 4 options:
  1. This month is the same as Start Date so count days from Start Date to end of month.
  2. This month is the same as End Date so count days from start of previous month to End Date.
  3. This month is between Start Date and End Date so count number of days in this month.
  4. This month is not a part of or between either Start Date or End Date so return zero.

So to deconstruct =IF(EOMONTH($A2,0)=EOMONTH(C$1,0),EOMONTH($A2,0)-$A2+1,IF(EOMONTH($B2,0)=EOMONTH(C$1,0),$B2-EOMONTH($B2,-1),IF(AND(C$1>$A2,C$1<$B2),EOMONTH(C$1,0)-EOMONTH(C$1,-1),0)))

(1). IF(EOMONTH($A2,0)=EOMONTH(C$1,0),EOMONTH($A2,0)-$A2+1 says if the last day of the month of the Start Date is the same as the heading month then get the date of the end month of the Start Date, subtract the Start Date and add 1.

(2). IF(EOMONTH($B2,0)=EOMONTH(C$1,0),$B2-EOMONTH($B2,-1) says if the last day of the month of the End Date is the same as the heading month then subtract the last day of the previous month of the End Date from the End Date.

(3). IF(AND(C$1>$A2,C$1<$B2),EOMONTH(C$1,0)-EOMONTH(C$1,-1) says if the heading date is greater than the Start Date AND the heading date is less than the End Date then return the end date of the current month minus the end date of the previous month of the heading (i.e. number of days in that month).

(4). ,0) says if none of the previous nested IF statements is true then return zero.
 
Upvote 0

Forum statistics

Threads
1,223,648
Messages
6,173,555
Members
452,520
Latest member
Pingaware

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