Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the type of a numeric value of a cell in Excel ALWAYS considered as DOUBLE?

Tags:

excel

vba

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?

like image 393
SoftTimur Avatar asked Jul 16 '13 20:07

SoftTimur


2 Answers

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.

like image 136
Santosh Avatar answered Nov 14 '22 21:11

Santosh


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.

like image 20
Andy G Avatar answered Nov 14 '22 22:11

Andy G