+ Reply to Thread
Results 1 to 9 of 9

Need formula to calculate days in each month across multiple Start and End dates

  1. #1
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Need formula to calculate days in each month across multiple Start and End dates

    Another Senior Moment, I'm afraid!

    Attached has four entries with Start and End dates, some of which span more than one month.

    Can't remember formula to "break and combine" the days between the relevant months to create table as shown in Cols H - J

    (E.g. Row 3 is 24 days between 14/02 - 09/03, 9 of which are in March. Row 5 then has 8 days between 14 - 21/03, so formula needs to identify the March total should be 17 days)

    All suggestions, pointers and solutions received gratefully as ever.

    Ochimus
    .
    Attached Files Attached Files
    Last edited by Ochimus; 10-25-2017 at 07:47 AM.

  2. #2
    Registered User
    Join Date
    10-12-2017
    Location
    Brazil
    MS-Off Ver
    2016
    Posts
    59

    Re: Need formula to calculate days in each month across multiple Start and End dates

    Hello.
    See if the example can help you.
    there may be difference of result due to the language of my excel but I believe this can be easily adjusted because the principle is the same.
    Attached Files Attached Files

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,755

    Re: Need formula to calculate days in each month across multiple Start and End dates

    Gfranco - sorry for off-topic interjection:

    Although there is no official rule regarding this behaviour, we request that wherever possible both the question AND the answer be provided in substantive detail here within the thread. An attached workbook is an excellent aid for posing a question and offering a solution, but solely doing that with no in thread explanation makes it difficult for researchers to understand or consider the Q & A of this thread without downloading what may be a pointless doc to them, if they can do that at all. Doing that also hides the content from search engines so others may never benefit from this.

    I'm sure you understand, and we look forward to seeing you post your formulas/macros in your posts for the searching benefit of all.

    Thanks again for all your hard work here!
    Last edited by AliGW; 10-25-2017 at 09:02 AM.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,480

    Re: Need formula to calculate days in each month across multiple Start and End dates

    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    That's just to extract the days from each month

  5. #5
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: Need formula to calculate days in each month across multiple Start and End dates

    Appreciate the fast responses, but unfortunately neither quite did what was needed.

    What does seem clear is that I have to analyse the data row by row, rather than having a "sumproduct" approach,

    The updated attachment uses that approach with a formula that hopefully "covers all the bases". It's in the attachment, but I can't show it here because, for whatever reason, the Site Firewall keeps blocking it, but it works as follows:

    Assume F1 is 1 Jan.

    If Out date is later than Jan, or the Return is earlier than Jan, cell is blank
    If Out date is earlier than Jan and the return is after the month end, cell is days in month
    If Out date is earlier than Jan, and the return is in Jan, cell is Return - Start of Month
    If Out date is within Jan, and the return is after the month end, cell is End of Month - Out date
    If Out and Return dates are within Jan, Return -Out

    Hopefully someone will flag up a shorter way to achieve it!

    Ochimus
    Attached Files Attached Files

  6. #6
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Need formula to calculate days in each month across multiple Start and End dates

    This formula in F2 copied down will give you the same results in your latest attachment, but with zeroes in place of blanks

    =MAX(0,MIN(EOMONTH(F$1,0),D2)-MAX(F$1,C2)+1)

    If you want the zeroes to look like blanks custom format column as 0;;


    It is possible, though, to get the results you suggested in your first workbook, with a single formula and no intermediate calculations

    Change the dates in H3 down so they are the 1st of each month (you can format as mmm-yy if you want to show Jan-17 etc.)

    Now in I3 you can use this array formula

    =SUM(IF($D$2:$D$5>=$H3,IF($C$2:$C$5<=EOMONTH($H3,0),IF($B$2:$B$5=I$2,IF($D$2:$D$5>EOMONTH($H3,0),EOMONTH($H3,0),$D$2:$D$5)-IF($C$2:$C$5>$H3,$C$2:$C$5,$H3)+1))))

    confirm with CTRL+SHIFT+ENTER and copy across to J3 and down, see attached
    Attached Files Attached Files
    Last edited by daddylonglegs; 10-25-2017 at 03:12 PM.
    Audere est facere

  7. #7
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: Need formula to calculate days in each month across multiple Start and End dates

    daddylonglegs,

    Many thanks for providing such an elegant solution!

    Appreciate greatly if you can just clarify the structure?

    I can follow that it adds the earlier date between the end of a month and the Return date, then deducts the later of either the start of the month or the Departure date, but why does it compare the result to a Zero to see which is greater?

    Ochimus

  8. #8
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Need formula to calculate days in each month across multiple Start and End dates

    If you just use this part....

    =MIN(EOMONTH(F$1,0),D2)-MAX(F$1,C2)+1

    You will get some negative results where the C2-D2 range doesn't intersect with that month at all, so MAX with zero means that all the negative results are converted to zeroes

    Note: I edited my earlier post to include a solution which gives your original table without individual results for each row

  9. #9
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: Need formula to calculate days in each month across multiple Start and End dates

    daddylonglegs,

    Not sure why your edit didn't show when I drafted my request for clarification, but needless to say I am extremely grateful that you produced exactly what I needed.

    Like so many others, the more I work with Excel the less I seem to know.

    Ochimus

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Need to calculate # of days per month between two dates even if end date is blank
    By jimthe5th in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 02-09-2023, 01:31 AM
  2. [SOLVED] Calculate year month and days between two dates
    By Premmadaan in forum Excel General
    Replies: 7
    Last Post: 04-24-2017, 05:38 AM
  3. How to calculate number of days between two dates by month
    By MHayward in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-06-2015, 12:26 PM
  4. [SOLVED] Calculate number of days in a month between two dates
    By schlomo87 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-04-2014, 12:32 AM
  5. Replies: 15
    Last Post: 06-03-2014, 02:08 PM
  6. Date Formula for days of the month, new dates starting on the 2nd of the month.
    By Kenn Jerger in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-04-2013, 01:31 AM
  7. [SOLVED] Formula needed to calculate working days in specified month between two dates
    By webfeet2 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-24-2013, 02:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1