Search notes:

VBA: Data types

All VBA variable data types are compatible with the variant datatype that is defined by OLE Automation.
The VBA data types are:
Name Comments Constant (vbVarType enumeration, returned by varType())
byte vbByte 17
boolean To store either true or false. vbBoolean 11
currency vbCurrency 6
date vbDate 7
decimal (not declarable) vbDecimal 14
double vbDouble 5
error (?: dim e as error is possible…) vbError 10
integer 4 bytes with sign vbInteger 2
long 4 bytes with sign vbLong 3
longLong 8 bytes (present only on 64-bit platforms) vbLongLong 20
longPtr
object such as excel objects, or user objects in class modules) (TODO: aka «object reference»? vbObject 9
single Other programming languages might call this data type a float vbSingle 4
string vbString 8
variant The actual type of the variable is determined at run time rather than compile time. Only used when added to vbArray. vbVariant 12
array This value is never returned by varType(), instead, the type-number of the elements in the array is added to 8192. vbArray 8192
null vbNull 1
empty vbEmpty 0
data access object vbDataObject 13
user defined types Data types that were declared with the type statement vbUserDefinedType 36
Note: the type statement allows to create a user defined datatype.
Each data type has a default value to which a variable is initialized when it is created.
Apparently, VBA does not have a char datatype. byte might be used instead.
A variable cannot be declared as decimal. Instead, the variable needs to be declared as variant and assigned to with cDec("42.1234").
Alternatively, a currency variable might be used which accurately stores up to four decimal places.
Except the byte datatype, there are no unsigned datatypes.
At runtime, the type of a variable can be determined with typeName(var) and varType(var).

Declaring data types of variables and constants

The data type of variables can be declared with the dim statement.
The deftype statements allows to specify default types depending on with which characters a variable starts, at module level.

Type-declaration character

A variable's data type can also be specified with the following symbols which are referred to as type-declaration characters.
  • %: integer
  • &: long
  • @: currency
  • !: single
  • #: double
  • $: string
These characters need to be appended to the variable name.

Simple example

option explicit

public   var_pub  as long           ' Declaring a public variable  (can be used in all modules)
private  var_prv  as date           ' Declaring a private variable (can only be used in THIS module)
const    c_word   = "foo"           ' Declaring a constant. Type of variable is determined implicitely

sub dimTest()

    dim var           as string     ' Declaring a "normal" variable
    dim str_3         as string * 3 ' Fixed-width string
    dim ary()         as integer    ' Declaring an array with variable count of elements
    dim ary_5(1 to 5) as integer    ' Declaring a fixed count of elements (here: 5)

  '
  ' Use type-declaration character to specify type for variables.
  ' Note, the type-declaration character is only required when the variable
  ' is declared, not when it is used (although it can optionally be appended
  ' to the variable name).
  '
    dim perc%
    dim excl!

    debug.print("type of perc is " & typeName(perc)) ' type of perc is Integer
    debug.print("type of excl is " & typeName(excl)) ' type of excl is Single
    
    call staticVar
    call staticVar

end sub

sub staticVar()

    static var as integer ' Static variables will keep their value accross sub/function calls
                          ' They are initialized with a default value (here: 0)
    
    debug.print "static var = " & var
    var = var + 1

end sub

See also

Data type conversion functions
VBA language
Convert a UTC text to a date
Compare with the Excel data types

Links

The VBA Language Specification: Data Values and Value Types

Index