+ Reply to Thread
Results 1 to 8 of 8

range.value = var ...unexpected results.

  1. #1
    Registered User
    Join Date
    07-18-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    36

    range.value = var ...unexpected results.

    Here's a code snippet of a routine that saves all properties of an object to cells in a row
    where r is a range (a single cell), obj is an object, prop is a String containing the name of a property of obj.
    For those not familiar with the function, the callbyname function just calls the getter for a property named by a string.. same as var=obj.prop (ie var=objPerson.Name)
    In this case the property returns data type Single.

    The var variable shows as Variant/Single in the locals window.

    Please Login or Register  to view this content.
    stepping through the debugger, after the last line of code executes, the immediate window shows the following:
    ?var
    0.565
    ?r.Value
    0.564999997615814

    I would like the cell to accurately reflect the value of the property whether the property is text, single, integer, etc.
    Anyone know why the change in precision on the assignment?
    Last edited by excel_joe; 06-21-2016 at 10:09 PM.

  2. #2
    Registered User
    Join Date
    07-18-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    36

    Re: range.value = var ...unexpected results.

    bump bump

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: range.value = var ...unexpected results.

    What exactly are obj and prop?

    Can you post the rest of the code and/or attach a sample workbook?
    If posting code please use code tags, see here.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: range.value = var ...unexpected results.

    It's not a complete answer, but I think the explanation is the difference between the "double" data type (r.value should be double, since Excel's inherent data type for numbers is double) and the "single" data type. Double precision carries about 15 digits of precision, single carries only 7 or 8 digits. 0.564999997615814 rounded to 8 digits is 0.565.

    I don't see it in the code you have posted, but, if you can figure out why "the property returns data type Single." and change it so that it returns data type double, that should fix this particular scenario.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: range.value = var ...unexpected results.

    Follow up to my previous post -- it is a simple type conversion issue. single 0.565, when converted to double, is 0.564999997615814. Test code:
    Please Login or Register  to view this content.
    If you execute the code, you will see (once it gets to the stop statement and enters debug mode) the value stored in dbvar and snvar, and they match what you show. Also note that, if you dim snvar as double instead of single (commented out dim statement), then they both are 0.565 instead of different.

    It look like this is a type conversion problem, so you need to figure out how you want to correct the type conversion. If we assume that 0.565000000000000 is the correct result, then we either need some way to force the 0.565 to be double at the outset, or figure out how to convert from single to double without changing the value. A Round() function could probably do the job after the fact, if that is the best way https://msdn.microsoft.com/en-us/lib.../gg278630.aspx

    I'm pretty sure that is why you have the change in precision. Best way to correct that probably depends on how readily you can change the dataype returned to var from single to double.

  6. #6
    Registered User
    Join Date
    07-18-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    36

    Re: range.value = var ...unexpected results.

    Thanks for the replies and insights. My function's intent is to take any property of any object (using the callbyname method) and save the property value to a cell. Therefore, I cannot change the original data type from what it is defined by the class.
    So my issue is how to have a var (which may be of any simple type - single, double, int, string) assign it to a cell using r.value=var . I have another function to read the cell value back but in essence the round trip shown below needs to return the orignal value of the var.

    Please Login or Register  to view this content.
    Should I use 'typeof' and then set the rng.number format based on the data type of the object's property (or var)?

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: range.value = var ...unexpected results.

    First thought -- is this really an error? The relative error between 0.565 and 0.564999... is about 5 parts per billion, a very small error. Is this error significant? Perhaps nothing special needs to be done at all.

    If this is determined to be an unacceptable error, then merely changing the number format is not likely to resolve the issue. Changing the numberformat, only changes the appearance of the value in a cell. It does not change the actual value of the cell. You can make this cell look like 0.565, but it will still contain the value 0.56499999....

    If you cannot change this when the value is assigned to var, I might consider a block If or Select Case to branch the code based on data type. Then, each branch of the code can perform the type conversion correctly. rng.value=round(cdbl(var),7)

    You might test other data types and see which ones have trouble. Off the top of my head, the other data types (double, long, integer, string) should not have any trouble with type conversion to Excel's two data types (double/string). It might be enough to simply test for typename(var)="single" to handle this special case, and let the other type conversions occur naturally.

  8. #8
    Registered User
    Join Date
    07-18-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    36

    Re: range.value = var ...unexpected results.

    Thanks. I use this field for the IRS Mileage rate which would then be used in a calc to determine the mileage reimbursent (currency type) to an employee. My customer freaked out when they save .564999999999
    in the cell. As an FYI, I basically have one sheet which acts as the persistent storage for all my class objects(ie person, location, etc) where each row is an instance of an object and the columns save the property values for that object instance.
    I can then load my high level object and it calls load for all of its children - instantiating all my objects back into memory. Same approach for saving - one call to the top object to save it and all its children.
    Thanks again for the fine advice.

+ 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] Unexpected results from MSQuery
    By j_Southern in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-10-2014, 11:09 AM
  2. [SOLVED] Find String in Range and Select it - unexpected results
    By TC1980 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-03-2013, 07:48 AM
  3. Unexpected results from CurrentRegion
    By ajgully in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-29-2011, 02:55 PM
  4. VLOOKUP with unexpected N/A results
    By cgjones in forum Excel General
    Replies: 2
    Last Post: 03-15-2011, 04:39 PM
  5. Formula giving unexpected results
    By johnmerlino in forum Excel General
    Replies: 6
    Last Post: 12-01-2010, 01:20 AM
  6. Unexpected Standard Deviations results
    By Shocked in forum Excel General
    Replies: 4
    Last Post: 12-02-2009, 11:42 AM
  7. Unexpected HLOOKUP results
    By scaffdog845 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2009, 03:01 PM
  8. Unexpected Results with CurrentRegion
    By avveerkar in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-06-2007, 09:57 PM

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