VBA: .Formula = "=" Cells.Value with a lot fo decimals

CedricMattelaer

New Member
Joined
Jun 16, 2011
Messages
37
Hello all,

This is a follow-up on a previous question (http://www.mrexcel.com/forum/showthread.php?p=2935323#post2935323), as I just now found out what seems to be causing the problem, I prefer to post it in a specific thread.

I have a formula which simplified (without loops and variables) goes as follows:
Code:
.Cells(592, 22).Formula = "= " & .Cells(592, 22).Value & " + " & .Cells(676, 18).Value
I want to add a specific value to the value already in that cell, and prefer to keep them written as a sum.

Now, sometimes the evaluation of this code results in an error. After long debugging, I found out that this happens in cells for which .Cells(592, 22) contains a lot of decimals (for example 0,999999999996).
If I change this number to 1, the program does what it should do without any problem.

I tried to set the decimal designator to . instead of a comma (",") but that doesn't make any difference.

Of course, I can first replace each cell by its rounded equivalent, but that's not very effective...

Is it normal that this happens? What is it due to? Is there any way around it?

If anyone has any clues, thanks for sharing!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Here's a simple idea, but often simple ideas work out quite well.

Code:
.Cells(592, 22).Formula = "=" & INT(.Cells(592, 22).Value) & " + " & .Cells(676, 18).Value
 
Upvote 0
If you want to read on that topic, just Google on

excel precision
or
floating point numbers

many articles are devoted to that topic, including very good ones.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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