Get value from cell without transform in vba macros

て烟熏妆下的殇ゞ 提交于 2020-01-05 03:34:28

问题


I have an issue with vba macros. Cell contains value 941144280284022000000. But when try to get this value in macros, variable is equal 9.41144280284022E+20. Is it possible to get "real" value? Thanks.


回答1:


Here is an example:

Sub dural()
    MsgBox ActiveCell.Text
End Sub

This will insure that long strings of numerals are not converted to numbers.

EDIT#1:

This assumes that the cell actually displays the long string of numerals. If the cell displays ####, that is what the sub will pick-up. If the cell displays 9.41E+20, then that is what my sub will pick-up.

My sub will not necessarily pick-up the contents of the Formula Bar.




回答2:


Try a simple :

Dim myVar as Variant

myVar = CDec(Range("A1").Value)



回答3:


If you wish to store a value that looks like a number (or is a number) but has more than 15 digits of precision, then you must either format the cell as text before entering the value, or prepend the value with a single apostrophe to indicate to Excel that the value is to be treated as text and not a number.

If you don't do this, then as soon as the value is entered, there's a good chance it will be altered by Excel. The trailing zeros in the example value mean this does not happen in this specific case, but try changing that last 0 to 1 and you'll see what I mean.

You enter: 941144280284022000001

Excel converts this to: 941144280284022000000

More reading: https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel



来源:https://stackoverflow.com/questions/41596316/get-value-from-cell-without-transform-in-vba-macros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!