1. Several different declarations: Public variables: Common variable name as data type
Private variable: Private variable name as data type
Static variable: Static variable name as data type
The Dim declaration is the most common declaration, including the above three types, which are different scopes for the variables to choose how to declare, as we learn the programming language, global variables and local variables.
2. Next scope: For example, our C language, global variables can be used throughout the program, generally have memory, and local variables are generally defined in the function, can only be used in the function, the function begins to allocate memory space, the function ends to free memory space.
In VBA, the scope of a variable refers to a module or process, with 3 levels:
Note that a public variable can only be declared in a Module object, if used in a form or sheet object, only as a module-level variable.
Table cannot be pasted
3. assign a value to the variable next
[let] variable name = Data let can be omitted
The assigned content, if it is text, is caused by "", and if it is a time date, it is written between two #.
Eg:
Sub Test ()
Dim Str as String
str = "Let's Swing Together, swing Together"
Range ("A1"). Value=str
End Sub
4. If the object is assigned a value (object type, such as cell, table): Set Variable name = Object
Eg:
Public Sub test1 ()
Dima as Range
Seta = Worksheets ("Sheet2"). Range ("B3")
A.value= "Object is a cell, which assigns a cell to a"
End Sub
?
5. Declaration and assignment of constants
Constants are used to store immutable values that need to be assigned at the same time as the declaration.
const constant Name as data type = numeric
Eg:const Pi as single=3.14
If you need to declare a module-level and public variable, refer to the preceding variable declaration.
Understanding the variable Declaration and assignment of VBA