In VBA, as this specification shows, numeric values can have several types: Double, Integer, Long, LongLong, Single, Decimal, Byte. However, it seems that in Excel, for a cell containing a numeric value, its type is always considered as Double.
There are several functions, either in Excel formula or VBA code, which can check data type of a cell. For instance, TypeName Function in VBA, TYPE function in Excel, IS functions in Excel, etc. But it seems that the finest type they can return for a numeric value is Double, other numeric types (Integer, Long, Byte...) can never be detected.
Could anyone tell me if I am right/wrong?
Reference - http://www.cpearson.com/excel/rounding.htm
Excel stores numbers differently that you may have them formatted display on the worksheet. Under normal circumstances, Excel stores numeric values as "Double Precision Floating Point" numbers, or "Doubles" for short. These are 8-byte variables that can store numbers accurate to approximately 15 decimal places. You may have only two decimal places displayed on the worksheet, but the underlying value has the full 15 decimal places.
Hence if you're reading a number from a cell, there's really no reason to use anything but a Double.
VBA's TypeName is correctly able to interpret Date
and Boolean
but, as you suggest, integers and decimal numbers are treated as Double
.
Excel and VBA are, to an extent, two different applications (essentially) and Excel cell-formatting doesn't directly correspond to VBA data-types.
We need to check the Value
's that we retrieve from Excel, and cast these values to an appropriate data-type if necessary. Similarly, when we pass information back to Excel we often have to Format the values, and/or format the cells that we are inserting them into.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With