Data Type Characters
Also known as Data Type Suffixes, Type Declaration Characters or Variable Declaration Characters.
This is not used so much know but does work and it saves space.
It is also possible to tell VBA the type of a variable by appending a special character to the end of the variable name.
Dim sFirstName$ 'String
Dim iNumber% 'Integer
Dim lAverage& 'Long
Dim sngTotal! 'Single
Dim dbTotal# 'Double
Dim cProfit@ 'Currency
Dim llDiscount^ 'LongLong on 64 bit
Type Declaration Characters
VBA does allow you to append a character to a variable's name as a quick way of indicating its data type.
This method of declaring variables should not be used and is only available for backwards compatibility purposes.
The following line will declare a variable with a Double data type
Dim dbDouble#
This line however should be declared using the "As" keyword.
Dim dbDouble As Double
Variable Declaration Characters
When you declare a literal constant or when you are performing a calculation you may want a specific data type to be used.
When you use the Variant data type the compiler is able to determine the correct data type for a numerical literal.
However there might be times when the default data type is different to the desired one.
Dim myVariant1 As Variant
Dim myVariant2 As Variant
myVariant1 = 100
myVariant2 = 100#
Debug.Print VBA.TypeName(myVariant1) 'Integer
Debug.Print VBA.TypeName(myVariant2) 'Double
Example
Dim myDouble As Double
Dim myInteger As Integer
myInteger = 32767
myDouble = myInteger * 100 ' the result is an Integer since both operands are Integer
Debug.Print myDouble ' error overflow
myDouble = myInteger * 100# ' tells it that 100 is a Double
Debug.Print myDouble ' 3276700
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext