+ Reply to Thread
Results 1 to 13 of 13

Sum Function Returns Zero But Works Properly Inside a Formula?

  1. #1
    Forum Contributor
    Join Date
    04-04-2023
    Location
    Detroit, Michigan
    MS-Off Ver
    365
    Posts
    170

    Sum Function Returns Zero But Works Properly Inside a Formula?

    So I understand that if the SUM function is used to total a range, and the data is formatted as text, it will return zero.

    But what I do not understand is why SUM works if used in a formula to total the results of other formulas that are returned as text.


    In the attached sheet I have the following text string in cell B3: PRX 19 1 - 2 - 1 $53,940

    Just to be clear, it is a record for a horse that raced at PARX racetrack. The horse raced 19 times, finished 1st once, second twice and third once. The total purse money won was $53,940.

    So I wrote this formula in cell B5 to calculate the percentage to show out of his 19 races, how often the horse was able to finish 1st, 2nd or 3rd.

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

    The formula works perfectly and the result = 0.210 or 21%

    But if the results of those TEXTAFTER(TEXTBEFORE formulas are text, not numbers, I don't understand why this formula doesn't return an error.

    So I dissected the formula above. Each section of the formula is now like this:

    F3 = TEXTAFTER(TEXTBEFORE(B3," ",3)," ",2) which returns 1
    F4 =TEXTAFTER(TEXTBEFORE(B3," - ",2)," - ",1) which returns 2
    F5 = TEXTAFTER(TEXTBEFORE(B3," $")," - ",2) which return 1

    Now in F8 the SUM function is used to calculate the total of the cells in the column F, Just like it summed the results of those same exact formulas in B5, and it returns zero!

    So I'm confused. Why does the formula in B5 return the correct result when it seems to be doing the same thing, summing text, not numbers?
    Attached Files Attached Files
    Last edited by JN831; 04-20-2024 at 01:02 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,495

    Re: Sum Function Return Zero But Works Properly Inside a Formula?

    I think it's because you are dividing the SUM of text values by a text value. The mathematical function coerces Excel into treating the text values as numeric values.

    Try:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    04-04-2023
    Location
    Detroit, Michigan
    MS-Off Ver
    365
    Posts
    170

    Re: Sum Function Return Zero But Works Properly Inside a Formula?

    @TMS I thought that might be the reason, also.

    But as shown in the formula below, if you remove the divide section at the end of the formula in B5, so that it only sums the results that are returned by the three TEXTAFTER(TEXTBEFORE formulas, it returns the correct result, 4.

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


    So that tells me that the dividing the SUM of text values by a text value is not what is causing Excel to treat the text values as numeric values.

    Also, to be clear I am not really looking for a solution as the formula in B5 is returning the correct result. I am looking to understand why Excel doesn't return an error or incorrectly calculate the result. Because I know SUM will not properly calculate the total of numbers formatted as text. Yet it seems to be doing just that in my formula. Hopefully someone can shed some light on this.
    Last edited by JN831; 04-20-2024 at 12:35 AM.

  4. #4
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,477

    Re: Sum Function Returns Zero But Works Properly Inside a Formula?

    If A1:A3 contains the number "1","2","3" as Text, then SUM(A1:A3) will =0
    However, if you sum each discrete cell as the following value: SUM("1","2","3") will = 6
    The formula you are using is also trying to separate 3 string numbers, then sum them together:
    =SUM("1","2","1")=4
    Since I don't have Ex365, I'll try testing with the MID function
    F3="PRX 19 1 - 2 - 1 $53,940", with numbers 1,2 and 1 at positions 8,12 and 16 respectively
    I will use:
    =SUM(MID(B3,8,1),MID(B3,12,1),MID(B3,16,1))=4
    (see attached file)
    Attached Files Attached Files
    Quang PT

  5. #5
    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
    81,204

    Re: Sum Function Returns Zero But Works Properly Inside a Formula?

    Just add -- to the start of each of the formulae in F3, F4 and F5 to coerce the text it returns into a number, e.g.

    =--TEXTAFTER(TEXTBEFORE(B3," ",3)," ",2)
    Attached Files Attached Files
    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.

  6. #6
    Forum Contributor
    Join Date
    04-04-2023
    Location
    Detroit, Michigan
    MS-Off Ver
    365
    Posts
    170

    Re: Sum Function Returns Zero But Works Properly Inside a Formula?

    Thank you Ali, yes I am aware that adding -- to the start of a formula coerces Excel to return text as a number.

    As I mentioned at the end of post #3, I am not really looking for a solution to fix my formula. The formula in B5 is returning the correct result.

    I wanted to ask for help in understanding why Excel doesn't return an error or incorrectly calculate the result in the formula I posted as it does not use the -- prefix in the formula. Yet it still seems to coerce the text it returns into a number.

    So basically I am asking why the formula in B5 returns the correct result without the -- in front of the formula.

  7. #7
    Forum Contributor
    Join Date
    04-04-2023
    Location
    Detroit, Michigan
    MS-Off Ver
    365
    Posts
    170

    Re: Sum Function Returns Zero But Works Properly Inside a Formula?

    @bebo021999 Using the MID function also returns the results as text and just like my formula, when used with the SUM function it calculates correctly. Interesting. Though I don't understand why.

    Like Ali mentioned, -- is the hack for this so why isn't it needed to coerce Excel to return the text it returns into a number?

    That's what I'm asking.

  8. #8
    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
    81,204

    Re: Sum Function Returns Zero But Works Properly Inside a Formula?

    I think it's what one would refer to as one of those undocumented features that sometimes creep into software.

    On the face of it, it should not work, as =SUM(F3,F4,F5) does not.

    If I were you, I'd not lose any sleep over it. You know, and that's all that matters.

    Or go and ask Microsoft.

    PS The common denominator would seem to be that you are using 365 functions if it doesn't coerce automatically with MID - 365 must handle native functions differently to legacy ones.
    Last edited by AliGW; 04-20-2024 at 02:02 AM.

  9. #9
    Forum Contributor
    Join Date
    04-04-2023
    Location
    Detroit, Michigan
    MS-Off Ver
    365
    Posts
    170

    Re: Sum Function Returns Zero But Works Properly Inside a Formula?

    I think you're spot on, Ali. I was hoping someone would report that there's a specific rule about this that I'm unaware of. Because as you said, on the face of it, it should not work.

    LOL, go ask Microsoft. Yeah, OK. Good one, Ali.

    One thing regarding MID is the formula presented by @bebo021999 does coerce correctly. I tested it.

    But I'm going to take your advice and try not to lose any sleep over this. My formula works despite -- not being added, and I reckon that's all that matters.

    I'm just a curious person, ask a lot of questions and want to know the "why" behind everything!

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,495

    Re: Sum Function Returns Zero But Works Properly Inside a Formula?

    For what it's worth, as well as using the double negative, you can add or subtract zero, or you can multiply or divide by 1.

  11. #11
    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
    81,204

    Re: Sum Function Returns Zero But Works Properly Inside a Formula?

    I am curious, too, but I know when it's time to stop!

    I misread the point about MID - so if it DOES coerce correctly, then it must just be to do with the formula being coerced inside another formula. Although, having said this, there will be occasions when it doesn't.

    I think the key thing here is knowing the hack and employing it when coercion does not happen automatically - by the way, I was quite justified in sharing the hack as there was no indication in your opening post (unless I've misread it) that you knew it.

  12. #12
    Forum Contributor
    Join Date
    04-04-2023
    Location
    Detroit, Michigan
    MS-Off Ver
    365
    Posts
    170

    Re: Sum Function Returns Zero But Works Properly Inside a Formula?

    @TMS Thank you. Yes, I am aware of the various ways to coerce Excel into returning text into a numeric value. However none of those are being used in my formula, yet it still works.

    Also, I replied in post#3 to your earlier post to show how the divide function in my formula is not the reason it works.

    So far nobody has been able to explain why the SUM function returns the SUM of text values and gives the correct result without using the double negative, adding or subtract zero, multiplying or dividing by 1.

    Like Ali said, perhaps it's just one of those undocumented features that sometimes creep into software.

    And like Ali said, perhaps it's time to stop and just let it go.

    Thank you to everyone who commented and tried to make sense out of this.

    I love this forum, everyone is so helpful.
    Last edited by JN831; 04-20-2024 at 03:16 AM.

  13. #13
    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
    81,204

    Re: Sum Function Returns Zero But Works Properly Inside a Formula?

    Yep - call it a day.

+ 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. How to use IF OR inside properly?
    By mreFF in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-28-2022, 09:55 PM
  2. Replies: 0
    Last Post: 01-19-2013, 01:35 PM
  3. Return a worksheet name inside a formula
    By kavehmj in forum Excel General
    Replies: 7
    Last Post: 10-26-2012, 01:31 PM
  4. Require information on how a specific formula works (see inside for formula)
    By twiggywales in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-04-2012, 10:13 AM
  5. [SOLVED] How to do an If function that does a sum inside it (or a formula that works!)
    By lembkin in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-29-2012, 07:57 AM
  6. How to remove a space before the "=" sign and make the formula works properly?
    By Leosado in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2010, 03:05 PM
  7. user defined function works in VBA but does not work inside worksheets
    By HabibValil in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-22-2008, 10:04 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