The VBA code contains variables, operators, and statements. Variables play a role in the code to cross and join. Variables are created as appropriate to the object and data type, to initialization, and then the operator to calculate or execute the statement modification, to complete the process of mutual exchange.
The key word for declaring a variable is dim, whose syntax is
Dim [WithEvents] varname[([subscripts])] [as [new] type] [, [WithEvents] varname[([subscripts])] [as [new] type]] ...
The syntax for the Dim statement contains the following sections:
Partial description
WithEvents is optional. keyword, which indicates that varname is an object variable used to respond to events triggered by an ActiveX object. It is only legal in the class module. With WithEvents, you can declare any desired single variable, but you cannot use WithEvents to create an array. New and WithEvents cannot be used together.
VarName is required. The name of the variable, followed by the standard variable naming convention.
subscripts is optional. The dimension of the array variable; You can define up to 60 dimensions of multidimensional arrays. The subscripts parameter uses the following syntax:
[Lower to] upper [, [Lower to] upper] ...
If lower is not explicitly specified, the lower bound of the array is controlled by the Option Base statement. If the option Base statement is not used, the lower bound is 0.
New is optional. You can implicitly create a keyword for an object. If you use new to declare an object variable, a new instance of the object is created the first time the variable is referenced, so you do not have to use the Set statement to assign a value to the object reference. The NEW keyword cannot declare variables of any intrinsic data type, as well as instances of dependent objects, nor can it be used with WithEvents.
Type is optional. The data type of the variable, which can be Byte, Boolean, Integer, Long, Currency, single, Double, Decimal (not currently supported), date, string (for variable length strings), String * length (For a fixed-length string), object, Variant, user-defined type, or object type. Each variable that you declare needs a separate as type clause.
While declaring a variable or creating a variable, it is best to consider what data type the variable is in, the table below shows the supported data types, and the size and scope of the storage space.
Data type storage space size range
Byte 1 bytes 0 to 255
Boolean 2 bytes True or False
Integer 2 bytes-32,768 to 32,767
Long
(long int) 4 bytes-2,147,483,648 to 2,147,483,647
Single
(Single-precision floating-point type) 4-byte negative numbers from -3.402823E38 to -1.401298E-45 when positive, from 1.401298E-45 to 3.402823E38
Double
(Double-precision floating-point type) 8-byte negative numbers from -1.79769313486231E308 to -4.94065645841247E-324 when positive, from 4.94065645841247E-324 to 1.79769313486232E308
Currency
(Variable-integer) 8 bytes from -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal 14 Bytes is +/-79,228,162,514,264,337,593,543,950,335 when there is no decimal point and 28 digits to the right of the decimal point/+ 7.9228162514264337593543950335; the smallest non-0 value is +/-0.0000000000000000000000000001
Date 8 bytes 100 year January 1 to December 31, 9999
Object 4 bytes Any object reference
String
(variable length) 10 bytes plus string length 0 to approximately 2 billion
String
(fixed length) string length 1 to approximately 65,400
Variant
(number) 16 byte any numeric value, up to a Double range
Variant
(character) 22 bytes plus string length has the same range as variable length string
User-defined
(using Type) the required number of all elements the range of each element is the same as the range of its own data type.
The variant is a very special data type that represents all values except the fixed length and can be returned by VarType to its data subtype. The syntax is as follows:
VarType (varname)
The necessary varname parameter is a VARIANT that contains any variables other than the user-defined type variable.