ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Small Decimal Won't Copy (https://www.excelbanter.com/excel-worksheet-functions/266232-small-decimal-wont-copy.html)

LarryAP

Small Decimal Won't Copy
 
I already have a workaround, but would like to understand why this was a problem (Excel 2007):

I have VBA code that is supposed to copy a row of numeric values into a corresponding range of cells in another worksheet.

Range("D13:P13").Cells(x).Value = cel.Offset(0, x).Value

I just found, though, that when one of the cells contains .0010, it comes across as .0000.

Both cells have the same number format, so I am nonplussed at this. My workaround, which solved the problem, is:

Range("D13:P13").Cells(x).Value = Val(CStr(cel.Offset(0, x).Value))

Somehow, casting the original value to a string, then back to a numeric value, makes it come out as .0010 as it should.

WHY was it not coming across correctly in the first place?

Sepeteus Jedermann

Quote:

Originally Posted by LarryAP (Post 959960)
I already have a workaround, but would like to understand why this was a problem (Excel 2007):

I have VBA code that is supposed to copy a row of numeric values into a corresponding range of cells in another worksheet.

Range("D13:P13").Cells(x).Value = cel.Offset(0, x).Value

I just found, though, that when one of the cells contains .0010, it comes across as .0000.

Both cells have the same number format, so I am nonplussed at this. My workaround, which solved the problem, is:

Range("D13:P13").Cells(x).Value = Val(CStr(cel.Offset(0, x).Value))

Somehow, casting the original value to a string, then back to a numeric value, makes it come out as .0010 as it should.

WHY was it not coming across correctly in the first place?

Hi,

I have Excel 2007 and cell formatting is general at whole worksheet.
I added some numbers to D11:D15 ( smallest was 0,00000005 ).
Then i selected continuos area, horizontal or vertical containing 5 cells.
After that i ran following vba-code. It worked ok.

All decimals at destination area was just like sourcearea ( D11:D15).
I think that excel make calculations with 15 decimals, always.

Sub TestSmallDecimal()

For x = 1 To 5
Selection.Cells(x).Value = Range("D10").Offset(x, 0).Value
Next x

End Sub

I dont know the reason why Excel works at your worksheet as you said, but
this exsample worked when I tried to reply your situation.

***


All times are GMT +1. The time now is 11:39 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com