How Excel handles Data Types

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
If you are curious about how Excel handles various Data Types, I did some research and created the following code:

VBA Code:
Sub RevealExcelDataTypeLengths()
'
'   The debate over how Excel treats Integer vs Long, etc. There is some belief, because of info posted on the internet, that
'   Integer is automatically converted to Long. Run the code below and see what the result is.
'
    Dim Boolean1        As Boolean
    Dim Boolean2        As Boolean
'
    Dim Collection1     As Collection
    Dim Collection2     As Collection
'
    Dim Double1         As Double
    Dim Double2         As Double
'
    Dim Integer1        As Integer
    Dim Integer2        As Integer
'
    Dim Long1           As Long
    Dim Long2           As Long
    Dim StorageBits     As Long
    Dim StorageBytes    As Long
'
    Dim Object1         As Object
    Dim Object2         As Object
'
    Dim Single1         As Single
    Dim Single2         As Single
'
    Dim Variant1        As Variant
    Dim Variant2        As Variant
'
'
'Boolean
    StorageBytes = VarPtr(Boolean1) - VarPtr(Boolean2)
    StorageBits = StorageBytes * 8
    Debug.Print "In this version of Excel, Boolean    is stored as:  " & StorageBytes & " bytes   which = " & StorageBits & " bits."
'
'Integer
    StorageBytes = VarPtr(Integer1) - VarPtr(Integer2)
    StorageBits = StorageBytes * 8
    Debug.Print "In this version of Excel, Integer    is stored as:  " & StorageBytes & " bytes   which = " & StorageBits & " bits."
'
'Collection
    StorageBytes = VarPtr(Collection1) - VarPtr(Collection2)
    StorageBits = StorageBytes * 8
    Debug.Print "In this version of Excel, Collection is stored as:  " & StorageBytes & " bytes   which = " & StorageBits & " bits."
'
'Long
    StorageBytes = VarPtr(Long1) - VarPtr(Long2)
    StorageBits = StorageBytes * 8
    Debug.Print "In this version of Excel, Long       is stored as:  " & StorageBytes & " bytes   which = " & StorageBits & " bits."
'
'Object
    StorageBytes = VarPtr(Object1) - VarPtr(Object2)
    StorageBits = StorageBytes * 8
    Debug.Print "In this version of Excel, Object     is stored as:  " & StorageBytes & " bytes   which = " & StorageBits & " bits."
'
'Single
    StorageBytes = VarPtr(Single1) - VarPtr(Single2)
    StorageBits = StorageBytes * 8
    Debug.Print "In this version of Excel, Single     is stored as:  " & StorageBytes & " bytes   which = " & StorageBits & " bits."
'
'Double
    StorageBytes = VarPtr(Double1) - VarPtr(Double2)
    StorageBits = StorageBytes * 8
    Debug.Print "In this version of Excel, Double     is stored as:  " & StorageBytes & " bytes   which = " & StorageBits & " bits."
'
'Variant
    StorageBytes = VarPtr(Variant1) - VarPtr(Variant2)
    StorageBits = StorageBytes * 8
    Debug.Print "In this version of Excel, Variant    is stored as:  " & StorageBytes & " bytes  which = " & StorageBits & " bits."
End Sub

You can check your results [URL='https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/data-type-summary']Here.[/URL]
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Office 365
1620267768462.png
 
Upvote 0
@mrshl9898, you would have to look into 'PtrSafe' to get that code to run on a 64Bit version of Office365. The point of the script is the results that it displays which should be similar to the following:

VBA Code:
In this version of Excel, Boolean    is stored as:  2 bytes   which = 16 bits.
In this version of Excel, Integer    is stored as:  2 bytes   which = 16 bits.
In this version of Excel, Collection is stored as:  4 bytes   which = 32 bits.
In this version of Excel, Long       is stored as:  4 bytes   which = 32 bits.
In this version of Excel, Object     is stored as:  4 bytes   which = 32 bits.
In this version of Excel, Single     is stored as:  4 bytes   which = 32 bits.
In this version of Excel, Double     is stored as:  8 bytes   which = 64 bits.
In this version of Excel, Variant    is stored as:  16 bytes  which = 128 bits.
 
Upvote 0
A post where I did a timing result between using Integer vs Long is located ---> here
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top