+ Reply to Thread
Results 1 to 19 of 19

Excel not recognizing values as equal

  1. #1
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Excel not recognizing values as equal

    Hi

    I have a spreadsheet which is exported by a vendor's software. This spreadsheet has a column of numbers, but I can't consistently work with them in vba. It seems like some of them are not recognized as numbers.

    As an example, I've stripped the sheet of all data except the column of numbers. Next I've set up a simple formula. Please see if you can make sense of why the formula returns "Not Equal". Changing the cell format does not help, and I also find it odd that if I set the cell format to number, changing the number of decimal places displayed does not effect what is shown in the cell.

    Thanks
    Attached Files Attached Files
    Last edited by davegugg; 04-23-2010 at 11:44 AM.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  2. #2
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Excel not recognizing values as equal

    your numbers are string.

    try =IF(H6=F11*1,"Equal","Not Equal")

    Does that help?


    click on the * Add Reputation if this was useful or entertaining.

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Excel not recognizing values as equal

    The values are text, not numbers. Here's an easy way to fix that.

    1) Put the number 1 in an empty cell
    2) Copy that cell
    3) Highlight all of the numbers to convert
    4) Select Edit > Paste Special > Multiply > OK
    5) Delete the cell you added, the cells have been converted to numbers
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    04-06-2010
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Excel not recognizing values as equal

    Hi, another way to convert text to numbers is to use Excel's error checking options. If you click in cell F9 you will see a small icon to the left with a triangle with an exclamation mark in the middle. If you don't see this then you may have disabled Error Checking in the Options - in xl 2003 ... Tools > Options > Error Checking tab > Enable Background Error Checking

    Click on this icon and you will see it has a menu - choose the Convert To Number option. Tip - if you select cells F9:F219 first then you can choose this option and convert all in one go.

    regards,
    Graham

  5. #5
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Excel not recognizing values as equal

    you might be able to change the import process so that they come in as numbers in the first place. why not attach a sample for us to look at.

  6. #6
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Excel not recognizing values as equal

    Ok, that makes sense, thank you all for the explanation.

    Now, what is the best way to convert in vba? I tried using cdbl() but it did not work.

  7. #7
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Excel not recognizing values as equal

    range("A1")=range("A1") * 1

  8. #8
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Excel not recognizing values as equal

    Thank you all. I have tried multiplying by 1 in my code, converting to number with the excel error checker, and converting to number by pastespecial multiplication, but the code I'm using still does not seem to recognize the values as the same. Unfortunately, do to personal data, I can't post my entire workbook, but here is a better version with my full code. Please take a look.

    The code attempts to compile two sheets into a third while putting multiple entries in sheet 2 as a comment in the Result sheet. I have set up a msgbox to inform the user if the amount in sheet one does not equal all corresponding amounts in sheet 2. Only amounts where "Equation Result" are included. It helps to have Result tab activated while running procedure, but not necessary.

    Thank you for your time
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Excel not recognizing values as equal

    try this

    Please Login or Register  to view this content.

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Excel not recognizing values as equal

    Hello Dave,

    I changed the macro to automatically change a value to double if it is a string and convert a number value to double. The problem is due to roundoff errors. I round the results to 5 digits. When I run the macro, I only get 2 loans that don not match up: 12655577 & 12690381. You should check the results yourself. Here is the modified macro. It has already added to the attached workbook.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  11. #11
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Excel not recognizing values as equal

    tony h:

    Did you try that, then run my "Compile" procedure? Because I just did and it did not work. The cells appear to have been changed to numbers, but my compile macro does not find the values equal, even though they clearly are when you step through the code.

    I'll have a look at that Leith, thanks

  12. #12
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Excel not recognizing values as equal

    Hi Leith

    That works great.
    Can you explain why roundoff errors were occurring when the numbers shown as a string all had 0 as the last digit shown? That doesn't make sense to me.
    Also, why did you need to use the Val() function instead of just being able to use cdbl()?

    Thanks again.

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Excel not recognizing values as equal

    Hello Dave,

    Because the numbers are double, the internal precision goes beyond the 15 digits Excel uses. VBA recognizes that minute difference and the test result is not equal.

    The Val statement converts a text string, if possible, into a double value. I use the VarType statement to identify if the cell value is text (value of 8) and than use Val to convert it. Otherwise I assume it is a valid number and use CDbl to make sure the number is a double value.

  14. #14
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Excel not recognizing values as equal

    ummmm...

    I see that my post is a bit late since the thread has evolved with Leith on the scene, but anyway, here's my alternative for changing the format of the initial list.

    Please Login or Register  to view this content.
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  15. #15
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Excel not recognizing values as equal

    But since the number (as a string) was -0.8500, wouldn't cdbl() change it to -.85? Are you saying it doesn't change it to EXACTLY that? Like it changes it to -.8500000000000000012?
    I don't see where the minute difference is coming from.

    It wasn't failing because it was still being evaluated as a string, was it?

  16. #16
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Excel not recognizing values as equal

    Hello Dave,

    If you go down to your IF statement that compares dblCumAdj and dblAdj, remove the rounding statement from each, and add a line that subtracts the two values you will see that that the result is not zero due to conversion of binary values to decimal values. If any of the decimals do not match then result is not equal.

  17. #17
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Excel not recognizing values as equal

    hi Dave,

    I don't have the understanding of Leith but I've read about floating point accuracy on a few sites which show some crazy examples. Check these out:
    http://blogs.msdn.com/excel/archive/...ue-update.aspx
    Chip's page is old, but I think the content is still correct for the latest version of Excel: http://www.cpearson.com/excel/rounding.htm
    http://www.thecodecage.com/forumz/ex...nt-errors.html

    Rob

  18. #18
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Excel not recognizing values as equal

    Leith and Boro, thank you very much for the explanation. I was not aware this issue existed with IEEE. Chip's page was most helpful, particularly the section on storing fractions in binary.

    I was even able to show the trick in 07 where the formula incorrectly shows 100,000 to some coworkers, and we all got a kick out of it.

    Thanks again

  19. #19
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Excel not recognizing values as equal

    LOL!

    Good as gold - thanks for the feedback, the rep', and marking the post as solved

    Rob

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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