+ Reply to Thread
Results 1 to 9 of 9

Need to be able to add and subtract months and days formatted as MM-DD text in excel

  1. #1
    Registered User
    Join Date
    06-15-2015
    Location
    Largo, FL
    MS-Off Ver
    2016
    Posts
    12

    Cool Need to be able to add and subtract months and days formatted as MM-DD text in excel

    Hi! I've been scratching my head on this one for awhile, and I'm pretty new to excel. I can reverse-engineer code to figure out what it is doing, but writing it is a whole other story...

    I need to create an Excel spreadsheet with the following rules:

    1. There are only 30 days in a month
    1. Users are able to enter a number of months and days as MM-DD in one single column
    2. The spreadsheet must be able to take those months and days, then add or subtract them to a result on another cell in the same column
    3. The result can have a maximum outcome of 130-29 (this result would round to "131 months" as seen in rule 5)
    4. The maximum number of days after the dash can only be 29 before rounding up to another month, so for example, 00-30 would equal 01-00, and 00-32 would equal 01-02
    5. The result would always round up to the greater month, so, for example, whether the result is 01-01 or 01-29, the result would always read "2 months"

    I can't get my head around this one. Maybe one of you gurus can help?

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: Need to be able to add and subtract months and days formatted as MM-DD text in excel

    Hi, welcome to the forum

    So, this is not really days and months, it is just 2 sets of values, that have certain restrictions placed on them?

    Can you upload a small (clean) sample workbook (not a pic) of what you are working with, and what your expected outcome would look like.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    06-15-2015
    Location
    Largo, FL
    MS-Off Ver
    2016
    Posts
    12

    Re: Need to be able to add and subtract months and days formatted as MM-DD text in excel

    Here you go: I tried to explain as best I could, let me know if it is not clear

    Example.xlsx

    Thanks so much for your time!

  4. #4
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Need to be able to add and subtract months and days formatted as MM-DD text in excel

    This formula would be one way for the 34-32 type numbers.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Unfortunately I cannot follow the logic of the rest of the sheet.
    Dave

  5. #5
    Registered User
    Join Date
    06-15-2015
    Location
    Largo, FL
    MS-Off Ver
    2016
    Posts
    12

    Re: Need to be able to add and subtract months and days formatted as MM-DD text in excel

    That gets me closer, but it's still too easy to break. What if I simply create a lookup table on another sheet, like this: Lookup Table for formula.xlsx

    How would the calculations work off that sheet? The excel spreadsheet would be larger but the formula should be much easier...

    Thanks for all your help so far!

  6. #6
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Need to be able to add and subtract months and days formatted as MM-DD text in excel

    Quote Originally Posted by Lynx_TWO View Post
    That gets me closer, but it's still too easy to break. What if I simply create a lookup table on another sheet, like this: Attachment 403577

    How would the calculations work off that sheet? The excel spreadsheet would be larger but the formula should be much easier...

    Thanks for all your help so far!
    Too easy to break. Have you tried to break it? Can you give an example of a "##-##" where it does break? I can see where a three digit leading number wouldn't work with the above formula (examples in the new lookup table)....but that can be modified.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    That works with 133-67 as well as 33-67. In cases where leading numbers are 1 to 12 the data itself will have to be entered as text (a leading ' should do it) or Excel would read your data as dates. Without that in those cases this formula won't work.

    How would the calculations work off that sheet?
    I don't see where the lookup table will be easier. For example there are no provisions for cases where the right numbers are larger than 29. Again even this recent formula can be modified to accommodate those right numbers that are larger than even 99. I.e. the lookup table would have to be exhaustive of all possibilities. I don't see "easier" with a lookup table. The math conversions still have to be made.

    BTW: Originally the data provided included only two digits on both sides of the "-". The above now takes care of 1, 2, 3, 4, 5 .... or more digits left of "-". How many digits do you anticipate right of "-"?

    A more thorough range of examples of the "-" combinations you anticipate would be helpful. Then we can see where the formula breaks.

  7. #7
    Registered User
    Join Date
    06-15-2015
    Location
    Largo, FL
    MS-Off Ver
    2016
    Posts
    12

    Re: Need to be able to add and subtract months and days formatted as MM-DD text in excel

    I think the problem still lies in adding or subtracting two or more values, and then turning the result. I have uploaded a small example here: Calculator broken example.xlsx The only way I can see to have excel input the format correctly is to use a custom mask, but then the issue lies in having to type in a number instead of being able to use text as an input...

    When you type in, say 23-21, excel turns this to a date without using text or a custom mask, but then when using text format, the SUM function will not work...

    There should only ever be two digits to the right, and a maximum of three digits to the left. The maximum total values when added will never be more than 128-29. Any value greater, (the next value would be 129-00) should give an error message.

    Basically there are simple defined rules for the calculation (of course this doesn't make it easy). Numbers to the right of the dash represent number of days, and numbers to the left of the dash represent number of months. There should only be 30 days in a month, which is why adding 23-24 and 01-17, the result should say 25-11 instead of 24-41.

    Does this make more sense?

    Thank you for your time!

  8. #8
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Need to be able to add and subtract months and days formatted as MM-DD text in excel

    If you weren’t aware of it formatting is cosmetic. It only changes the presentation of numbers into forms that we can recognize as readable. They mean nothing to Excel. Excel sees only numbers.

    If there is a way to invent a custom format built upon a custom numbering system and retain the real values I am unaware of it. You have a custom numbering system that is a mix of base 10 and base 30. As far as I can see your “formatting” will need to be coerced in the last step and not through Format Manager. The downside is that the resulting numeric values will not be there. You will need to resort to string manipulations to order to extract usable numbers at that point.

    In the attached I reset the workbook formatting to General. Setting it to Text introduces complications that are a real pain …… especially if you are going to mix the data as text and numbers as in your last upload.

    I then re-entered the data as 3 numbers followed by “-“ followed and then followed by 2 numbers …. leading zeroes included. Excel will read these as text by default without need of invoking Format Manager.
    The below shows what I mean.

    Row\Col
    E
    F
    G
    H
    1
    004-11 004-11 004-11
    2
    128-11 128-22 000-23
    3
    132-22 132-33 004-34 In E3:G3 =TEXT(SUM(LEFT(E1,3),LEFT(E2,3))&SUM(RIGHT(E1,2),RIGHT(E2,2)),"000-00")
    4
    5
    132-22 133-03 005-04 In E3:G3 =TEXT(LEFT(E3,3)+INT(RIGHT(E3,2)/30),"000")&TEXT(MOD(RIGHT(E3,2),30),"-00")


    Does this look like what you want?

  9. #9
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Need to be able to add and subtract months and days formatted as MM-DD text in excel

    I've been working on another approach that uses helper columns to isolate the math steps from the "formatting" that I mentioned in my last post.

    Row\Col
    A
    B
    C
    D
    E
    F
    G
    1
    Months-Days Days only Months-Days Days only Months-Days Days only
    2
    004-11
    131
    044-13
    1333
    002-48
    108
    In B2:B3, D2:D3 & F2:F3 =SUMPRODUCT(LEFT(A2,3)*30)+SUMPRODUCT(--RIGHT(A2,2))
    3
    132-67
    4027
    000-02
    2
    045-01
    1351
    4
    5
    Total Days
    4158
    1335
    1459
    In B5, D5 & F5 =SUM(B2:B3)
    6
    Converted 138-18 044-15 048-19 In B6, D6 & F6 =TEXT(INT(B5/30)&MOD(B5,30),"000-00")
    Last edited by FlameRetired; 06-29-2015 at 09:22 PM. Reason: Corrections in formula description and file.

+ 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] Excel Macro: Ask user for date then subtract 6 months from it
    By foxy_420 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-21-2013, 11:09 AM
  2. [SOLVED] Returning 'Number' of 'Days' or 'Weeks' or 'Months' with # and text
    By PERE in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-30-2013, 08:45 PM
  3. Replies: 15
    Last Post: 06-26-2012, 07:28 AM
  4. Converting text-formatted dates into months
    By pondorpool in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 03-09-2010, 09:43 PM
  5. Subtract dates-show how many months & days
    By AlmostAGeek in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-28-2007, 08:59 PM

Tags for this Thread

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