Data type conversion in VBA
Open the object browser in VBE, and operate according to the following figure:
Find the VBA library and enter Conversion (conversion) to find the module, which provides data conversion functions.
VBA provides two sets of built-in data type conversion functions. The first set contains Int, CVDate and Str. This is an early version, which is only reserved for backward compatibility. The second group of functions all start with the letter "C", and the second group is used in programming.
The syntax of each conversion function is basically the same, for example:
Expression is an input parameter, and then converted to the corresponding data type.
VBA supports data type conversion functions:
VBA.Conversion.CBool(Expression) as Boolean
Convert the variable Expression to Boolean data type. Expression can be any numeric type or any string that can be converted to numeric type. If expression = 0 or “0”, it returns False, otherwise it returns True(-1).
VBA.Conversion.CByte(Expression) as Byte
Convert the variable expression to the Byte data type. The expression can be any numeric type or any string that can be converted to a numeric type. The range of Expression is 0-255. If exp exceeds the range, an error will be displayed. If exp is a floating point number, then before converting to byte, it is rounded and then converted.
VBA.Conversion.CDec(exp)
Convert exp to Decimal subtype. Exp can be any numeric value or string in the Decimal range. This function is the only class that can produce Decimal data in VBA.
VBA.Conversion.CDate(exp) as Date
Convert exp to Date/Time data type. exp is a date number and a string representing the date and convert it to the date format represented by the computer. For example: on a computer set to the American format mm/dd/yy, if the British date grid dd/mm/yy is converted using CDate, it will be converted to the American date format.
VBA.Conversion.CCur(exp) as Currency
Convert exp to currency data type, exp can be converted value and string. This function encodes decile and thousandth according to the computer language environment.
VBA.Conversion.CInt(exp) as Integer
Convert exp to Integer type, exp can be any numeric value and string representing the range of Interger.
VBA.Conversion.CLng(exp) as Long
Convert exp to Long type, exp can be a numeric value and a string in the range of Long.
VBA.Conversion.CSng(exp) as Single
Convert exp to Single type, exp can be a single value and string in the range.
VBA.Conversion.CStr(exp) as String
Convert exp to String type, exp can be of any data type
VBA.Conversion.CVar(exp) as Variant
Convert exp to Variant type, exp can be any data type
Variant variant data type
VBA contains a special data type namely Variant (variant) type. It allows use with any data type in VBA and automatically calculates the data type closest to the assignment type, but performance and code reading are affected.
Variant special subtype
Variant can represent all the built-in data types, this variant also supports the following special data types.
Empty
Before declaring the new variable of Variant without assigning a value, the Empty subtype will be automatically assigned to the variable.
The subtype of var1 is Empty, and it is Empty before var2 is assigned. Can display the assignment of null values.
Null
Null is a special subtype, which means that the variable does not contain any valid data. In order for the type to be Null, the variable must be explicitly assigned Null.
Error
The error subtype is used to store the error number, which is automatically generated by VBA, and then used by the error handler.
Determine the specific data type represented by Variant
It is good to let the variable Variant replace all data types, but sometimes to determine the actual data type stored in the variable, VBA provides two functions: VBA.Information.VarType returns an integer to determine the specific data type. VBA.Information.TypeName returns a string of specific data types.
VarType
Syntax:
VBA.Information.VarType(exp) as VBA.VbVarType
Exp Need to determine the subtype of the variable, the following is the specific subtype it returns
return value | Subtype | Enumeration symbol |
---|---|---|
0 | Empty | vbEmpty |
1 | Null | vbNull |
2 | Integer | vbInteger |
3 | Long | vbLong |
4 | Single | vbSingle |
5 | Double | vbDouble |
6 | Currency | vbCurrency |
7 | Date | vbDate |
8 | String | vbString |
9 | OLE Automation object | vbObject |
10 | Error | vbError |
11 | Boolean | vbBoolean |
12 | Array of Variant | vbVariant |
13 | Data access object | vbDataObject |
14 | Decimal | vbDecimal |
17 | Byte | vbByte |
36 | User-defined Type | vbUserDefinedType |
8192 | Array | vbArray |
In fact, the VarType function does not return 8192. As mentioned in the table above, when an array variable is passed, VarType returns the sum of 8192 and the specific type value of the array.
TypeName
This function returns the string form of the specific data type.
Variant and strong typing
Variant can meet all data type requirements, but performance will decrease. Compared with the determined data type, the processing speed is about 30% slower.
Return Variant and strongly typed functions
The VBA language contains many string processing functions, they have two versions, one returns Variant, the other returns a string. The name of the function that returns a string ends with a dollar sign. Through comparison, it is found that the execution speed of the function returning Variant is about 30% slower than the function returning string. It is strongly recommended to use functions ending in $.
The above is the basic situation of VBA data type conversion. If interested, add a group: 794568082 to discuss and learn from each other.