+ Reply to Thread
Results 1 to 13 of 13

Formula to count the number of different months in a range

  1. #1
    Registered User
    Join Date
    07-24-2013
    Location
    Nottingham,England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Formula to count the number of different months in a range

    Hi all,

    I am looking to populate a cell with a value based upon the number of different months in a column of data containing dates.

    Example:

    6-Sep-12
    14-Sep-12
    25-Sep-12
    3-Oct-12
    19-Oct-12
    6-Nov-12
    8-Nov-12
    26-Nov-12

    The above list contains various dates representing three different months. Therefore the answer to the formula I require based on that range of data would be 3.

    Many thanks

  2. #2
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Formula to count the number of different months in a range

    Could your requirement be reworded as

    Find the Earliest Date and the Latest Date.
    And you want the number of months between those dates?

    Or would there ever be a chance that you have dates in September and November, but none in October, therefore it would only be a count of 2 ?

  3. #3
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Formula to count the number of different months in a range

    Hi,

    Assuming the data you give above is in A2:A9, this array (important!) formula will give you your desired result:

    =SUM(1/(MMULT((TRANSPOSE(ROW(A2:A9)^0)),--(MONTH(A2:A9)=TRANSPOSE(MONTH(A2:A9))))))

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  4. #4
    Registered User
    Join Date
    07-24-2013
    Location
    Nottingham,England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Formula to count the number of different months in a range

    Quote Originally Posted by Jonmo1 View Post
    Could your requirement be reworded as

    Find the Earliest Date and the Latest Date.
    And you want the number of months between those dates?

    Or would there ever be a chance that you have dates in September and November, but none in October, therefore it would only be a count of 2 ?
    Jonmo1,

    Many thanks for the quick response.

    Your last statement is correct. There could be months missing from the data.

    Regards,

  5. #5
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Formula to count the number of different months in a range

    Quote Originally Posted by Fishkeeper View Post
    Your last statement is correct. There could be months missing from the data.
    Then go with XOR LX's solution.
    You have to enter it with CTRL + SHIFT + ENTER

  6. #6
    Registered User
    Join Date
    07-24-2013
    Location
    Nottingham,England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Formula to count the number of different months in a range

    Quote Originally Posted by XOR LX View Post
    Hi,

    Assuming the data you give above is in A2:A9, this array (important!) formula will give you your desired result:

    =SUM(1/(MMULT((TRANSPOSE(ROW(A2:A9)^0)),--(MONTH(A2:A9)=TRANSPOSE(MONTH(A2:A9))))))

    Regards

    XOR LX,

    Works perfectly. However, if I extend the range to compensate for yet to be populated cells it returns a greater value as it seeing blank cells as another month.

    To explain:

    If the current data (containing sept, oct, nov) is in A2:A9 the result is 3. If I extend the range on the formula to A100 to allow for future data entry. The formula returns 4.

    Thanks

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

    Re: Formula to count the number of different months in a range

    Try this formula, it can tolerate blanks in the range and dates in different years

    =SUM(IF(FREQUENCY(A2:A100,A2:A100-DAY(A2:A100)),1))

    confirmed with CTRL+SHIFT+ENTER
    Audere est facere

  8. #8
    Registered User
    Join Date
    07-24-2013
    Location
    Nottingham,England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Formula to count the number of different months in a range

    daddylonglegs,

    just the ticket. Thank you very much.

    Many thanks to all who responded to my post. As a first-time poster this was a great experience.

    Hopefully, I can help you all some time.


  9. #9
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Formula to count the number of different months in a range

    "...and dates in different years"

    Are you sure?

    Regards

  10. #10
    Registered User
    Join Date
    07-24-2013
    Location
    Nottingham,England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Formula to count the number of different months in a range

    yep, just checked it by adding dates from 4 different years and the count increased by 1 each time I add a date

    Regards,

    p.s. who marks posts SOLVED? Is it myself (how?) or moderators?

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

    Re: Formula to count the number of different months in a range

    Quote Originally Posted by XOR LX View Post
    Are you sure?
    Yep!

    What I mean is that it will count 3-Sep-2012 and 22-Sep-2012 as the same (because they are both September 2012) but it will count 4-Sep-2013 and 15-Sep-2012 as different (i.e. count twice) because the two dates are in different Septembers

  12. #12
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Formula to count the number of different months in a range

    "Works perfectly. However, if I extend the range to compensate for yet to be populated cells it returns a greater value as it seeing blank cells as another month."

    Why don't you simply make the range dynamic then, so that it adjusts to match the number of populated cells in the range at any given time? So, for example, go to Name Manager and define a new name, Range say, and in the Refers to: box enter:

    =Sheet1!$A$2:INDEX(Sheet1!$A:$A,MATCH(7^7,Sheet1!$A:$A))

    Then my formula becomes:

    =SUM(1/(MMULT((TRANSPOSE(ROW(Range)^0)),--(MONTH(Range)=TRANSPOSE(MONTH(Range))))))

    Regards

  13. #13
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Formula to count the number of different months in a range

    Then apologies to both of you.

    I read this: "I am looking to populate a cell with a value based upon the number of different months" as meaning months in different years were to be considered the same.

    Which means of course that my solution is incorrect.

    Regards

+ 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] Count Months within Range
    By maverickballa in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-15-2013, 12:13 PM
  2. [SOLVED] Count the number of whole calandar months in a date range
    By robfresh in forum Excel Formulas & Functions
    Replies: 22
    Last Post: 10-19-2012, 05:28 AM
  3. [SOLVED] Can i count the number of months in a specific range
    By radicrains in forum Excel General
    Replies: 12
    Last Post: 09-21-2011, 03:06 AM
  4. Count Number Of Elapsed Months
    By simonyglog in forum Excel General
    Replies: 4
    Last Post: 04-08-2011, 10:34 AM
  5. count number of months year to date
    By coal_miner in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-04-2005, 10:06 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