Home » excel » Excel VBA transferring numbers shouldn't round off decimals

Excel VBA transferring numbers shouldn't round off decimals

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m “copying” a row of a table to another location (range, not a table), using this method:

rDestination.value = TableRow.value

The last cell of the row is fraction of a dollar, such as $0.01925. But when the value is copied to it’s destination, it reads $0.02.

It’s not just a format thing; I look inside the cell and it really does say $0.02.

(EDIT: The problem only seems to occur when the source cell is in currency format and displaying two decimals. That is, the actual value is $0.01925 but the cell is formatted $0.02.)

I’ve tried using this code both before and after the other one:

 rDestionation.cells(5).NumberFormat = "0.00000"

and also

rDestination.Cells(5).NumberFormat = TableRow.Cells(5).NumberFormat 

But they don’t work. (Not surprisngly, since it’s not actually the format being affected).

How to&Answers:

Use .Value2 instead of .Value.

Here is a blog post that explains the differences between the 2 (namely when you are dealing with currency): https://fastexcel.wordpress.com/2011/11/30/text-vs-value-vs-value2-slow-text-and-how-to-avoid-it/

As mentioned there:

.Value2 really should be the default, and is definitely the one
to use 99% of the time.