Value changed when posted to cell

Atroxell

Active Member
Joined
Apr 18, 2007
Messages
422
HI,

I have a macro that places a values into a variable named "Yield". The value is safely passed to the variable. When the value is later posted to a different cell, the value is slightly different. When using the watch window, I see the variable correctly housing the value, even after posting it to the cell.

By example:

Code:
...
Dim Yield as Single
.
.
.
Yield = ActiveCell.Offset(0, 1).Value        [COLOR="Red"] 'At this point the value is 0.4[/COLOR]
.
.
.
ActiveCell.Offset(-2, 1) = Yield              [COLOR="red"] ' Suddenly, the value is 0.400000005960464 when viewed in the cell.[/COLOR]
...

What causes it and how do I correct this?

TIA
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi Atroxell,

Try declaring Yield as a Double data type instead of a Single.

My understanding is that Excel stores cell values as Doubles, so when you read a cell value into a Single, a conversion must be done.

(Why Excel converts 0.4 to 0.400000005960464 is beyond my understanding). :eeek:

Nonetheless, if you use a Double, it will work correctly.
 
Upvote 0
Wow. That simple! Worked perfectly.

One of these days I am REALLY going to sit down and read ALL of the info on data types...

Thanks for your response!
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,259
Members
452,901
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top