Skip to content

Instantly share code, notes, and snippets.

@sancarn
Last active July 5, 2019 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sancarn/985e9e7b23d1395651b0240987aa357a to your computer and use it in GitHub Desktop.
Save sancarn/985e9e7b23d1395651b0240987aa357a to your computer and use it in GitHub Desktop.
VBA Pointers and SizeOf()
  Dim x01 As Boolean
  Dim x02 As Byte
  Dim x03 As Currency
  Dim x04 As Date
  Dim x05 As Double
  Dim x06 As Integer
  Dim x07 As Long
  Dim x08 As LongPtr
  Dim x09 As MsoRGBType
  Dim x10 As Object
  Dim x11 As OLE_CANCELBOOL
  Dim x12 As OLE_COLOR
  Dim x13 As OLE_HANDLE
  Dim x14 As OLE_OPTEXCLUSIVE
  Dim x15 As Single
  Dim x16 As String
  Dim x17 As Variant
  Dim x18 As Integer 'Random for offset
  
  Debug.print "| Variable | Type             |Size|Len|LenB|"
  Debug.print "|----------|------------------|-|-|-|"
  Debug.Print "| x01      | Boolean          |" & Abs(VarPtr(x02) - VarPtr(x01)) & "|" & Len(x01) & "|" & LenB(x01)  & "|"
  Debug.Print "| x02      | Byte             |" & Abs(VarPtr(x03) - VarPtr(x02)) & "|" & Len(x02) & "|" & LenB(x02)  & "|"
  Debug.Print "| x03      | Currency         |" & Abs(VarPtr(x04) - VarPtr(x03)) & "|" & Len(x03) & "|" & LenB(x03)  & "|"
  Debug.Print "| x04      | Date             |" & Abs(VarPtr(x05) - VarPtr(x04)) & "|" & Len(x04) & "|" & LenB(x04)  & "|"
  Debug.Print "| x05      | Double           |" & Abs(VarPtr(x06) - VarPtr(x05)) & "|" & Len(x05) & "|" & LenB(x05)  & "|"
  Debug.Print "| x06      | Integer          |" & Abs(VarPtr(x07) - VarPtr(x06)) & "|" & Len(x06) & "|" & LenB(x06)  & "|"
  Debug.Print "| x07      | Long             |" & Abs(VarPtr(x08) - VarPtr(x07)) & "|" & Len(x07) & "|" & LenB(x07)  & "|"
  Debug.Print "| x08      | LongPtr          |" & Abs(VarPtr(x09) - VarPtr(x08)) & "|" & Len(x08) & "|" & LenB(x08)  & "|"
  Debug.Print "| x09      | MsoRGBType       |" & Abs(VarPtr(x10) - VarPtr(x09)) & "|" & Len(x09) & "|" & LenB(x09)  & "|"
  Debug.Print "| x10      | Object           |" & Abs(VarPtr(x11) - VarPtr(x10)) '& "|" & Len(x10) & "|" & LenB(x10) & "|"
  Debug.Print "| x11      | OLE_CANCELBOOL   |" & Abs(VarPtr(x12) - VarPtr(x11)) & "|" & Len(x11) & "|" & LenB(x11)  & "|"
  Debug.Print "| x12      | OLE_COLOR        |" & Abs(VarPtr(x13) - VarPtr(x12)) & "|" & Len(x12) & "|" & LenB(x12)  & "|"
  Debug.Print "| x13      | OLE_HANDLE       |" & Abs(VarPtr(x14) - VarPtr(x13)) & "|" & Len(x13) & "|" & LenB(x13)  & "|"
  Debug.Print "| x14      | OLE_OPTEXCLUSIVE |" & Abs(VarPtr(x15) - VarPtr(x14)) & "|" & Len(x14) & "|" & LenB(x14)  & "|"
  Debug.Print "| x15      | Single           |" & Abs(VarPtr(x16) - VarPtr(x15)) & "|" & Len(x15) & "|" & LenB(x15)  & "|"
  Debug.Print "| x16      | String           |" & Abs(VarPtr(x17) - VarPtr(x16)) & "|" & Len(x16) & "|" & LenB(x16)  & "|"
  Debug.Print "| x17      | Variant          |" & Abs(VarPtr(x18) - VarPtr(x17)) & "|" & Len(x17) & "|" & LenB(x17)  & "|"

Result:

Variable Type Size Len LenB
x01 Boolean 2 2 2
x02 Byte 8 1 1
x03 Currency 8 8 8
x04 Date 8 8 8
x05 Double 2 8 8
x06 Integer 6 2 2
x07 Long 4 4 4
x08 LongPtr 4 4 4
x09 MsoRGBType 4 4 4
x10 Object 2
x11 OLE_CANCELBOOL 6 2 2
x12 OLE_COLOR 4 4 4
x13 OLE_HANDLE 2 4 4
x14 OLE_OPTEXCLUSIVE 6 2 2
x15 Single 4 4 4
x16 String 16 0 0
x17 Variant 2 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment