Each variable has its own data type (e.g., numeric, string). This section mainly introduces basic data types and type conversion in Excel VBA and Python.
Basic Data Types
Excel VBA
Common data types in Excel VBA include Boolean, Byte, String, Date, Variant, and Object, as shown in Table 2-2.
Table 2-2: Common Data Types in Excel VBA
For a given variable, Excel VBA provides functions to check its data type. Use TypeName and VarType to return the data type name and value of the variable. Use IsNumeric to check if the variable is numeric or currency type, IsDate to check if it is a date type, IsEmpty to check if it has been initialized, and IsNull to check if it has a valid value. The sample file path is Samples\ch02\Excel VBA\DataTypes.xlsm.
Sub Test()
Dim intA As Integer
Dim bolB As Boolean
Dim strC As String
Dim datD As Date
Dim varF As Variant
intA = 8
bolB = True
strC = "Hello"
datD = "05/25/2021"
varF = Null
Debug.Print TypeName(bolB) ' Returns the data type name of bolB
Debug.Print VarType(intA) ' Returns the data type of intA(as a number)
Debug.Print IsNumeric(intA) ' Checks if intA is numeric
Debug.Print IsDate(datD) ' Checks if datD is a date
Debug.Print IsEmpty(lngE) ' Checks if lngE is initialized
Debug.Print IsNull(varF) ' Checks if varF has a valid value
End Sub
Running the subroutine outputs the following in the Immediate Window:
Boolean
2
True
True
True
True
VarType returns 2, indicating intA is a short integer. Note: If lngE is not declared, IsEmpty returns True (not initialized); if declared, it returns False (initialized).
Python
Common data types in Python include Boolean, numeric, string, list, tuple, etc., as shown in Table 2-3. Thus, Python 3 has no distinction between short/long integers or single/double-precision floats.
Table 2-3: Common Data Types in Python
In Python, use the type function to return a variable’s data type. You can also use isinstance to check if a variable is a specified data type.
>>> a = 12.3
>>> b = 'Hello'
>>> type(a)
<class 'float'>
>>> isinstance(b, str)
True
type shows a is a float, and isinstance confirms b is a string.
Data Type Conversion
Excel VBA
In Excel VBA, type conversion has two methods: explicit and implicit. Explicit conversion uses a series of conversion functions (listed in Table 2-4). These functions usually start with C, followed by the abbreviation of the target data type (e.g., CInt for short integer, where Int is the abbreviation of Integer).
Table 2-4: Conversion Functions in Excel VBA
Below, use CSng to convert a short integer to a single-precision float:
Dim intA As Integer
Dim sngB As Single
intA = 10
sngB = CSng(intA)
You can also use implicit conversion:
sngB = intA
Python
Common type conversion functions in Python are listed in Table 2-5.
Table 2-5: Common Type Conversion Functions in Python
Examples of type conversion:
>>> a = 10
>>> b = float(a) # Convert to float
>>> b
10.0
>>> type(b)
<class 'float'>
>>> c = complex(a, -b) # Create complex number with a and b
>>> c
(10-10j)
>>> type(c)
<class 'complex'>
>>> d = str(a) # Convert to string
>>> d
'10'
>>> type(d)
<class 'str'>
After type conversion, a new object is created in memory, not modifying the original object’s value. Use id to check the memory addresses of a, b, c, and d:
>>> id(a)
8791516675424
>>> id(b)
51490992
>>> id(c)
51490960
>>> id(d)
49152816
Thus, each variable has a different memory address—conversion creates a new object.