VBA Intermediate class 1 Summary

Finally begin the long-awaited VBA intermediate class! Is proud to be the first batch of students Intermediate classes, now let's start VBA new journey now!

1 variable naming rules

1.1 Variable

Some data will change in the execution of the program, for which we allocate a memory location to store the data in these uncertain, which is variable. Each variable has a variable name must be unique within the same range. You can specify the data type, you can not do.

1.2 Variable naming rules

Variable names must begin with a letter (Chinese version can be the beginning of the Chinese), composed of letters, digits, and underscores (Chinese version can be used in Chinese), not more than 255 characters (the same for all versions). Note that, the variable name can not be a reserved name system, such as if, else, end and so on.

  • VBA variable names are not case sensitive

2 to declare variables

Declare variables that determine the variable name and specify the data type of the variable. By definition statements can be divided into explicit and implicit declarations.

2.1 explicit declaration

That is, first declare a variable type, and then reference variables, the syntax is:

         Public | Static | Private | Dim < variable name > [as  data types ]

    Variable names : Variable used to refer to

Data Type : determining a type of variable size and memory for storing information

  • [as Data Type ] can be omitted, when omitted, the VBA automatic variables specified as Variant data type (the Variant) is.

2.1.1 type declaration characters

You can use some of the characters in VBA to represent data types, simplify declare a variable, this is the type declaration characters, not all data types are type-declaration character, the following table shows all the type declaration characters:

type of data

Type declaration characters

String (String)

$

Integer (Integer)

%

Long integer (Long)

&

Single-precision floating-point (Single)

!

Double precision floating point type (Double)

#

Currency (Currency)

@

For example:

IRow & Dim     'declare long integer variable iRow

2.1.2 declare multiple variables simultaneously

When you declare a variable, if there are multiple variables, we can use a comma to separate different variables:

Dim < variable name. 1 > [AS  Data type ], < variable name 2 > [AS  Data type ], ..................

  • Each variable must specify the data type, the data type corresponds to omit designated Variant.

Example: Dim i, j AS long     'is represented by a Variant i and j are long type

2.2 implicit declaration

This variable is declared in advance that is not before the procedure reference variables. In this case, VBA is automatically assigned to the variable Variant data types. Because most widely Variant type, memory capacity, it can store all types of data in addition to customizing type and fixed-length string user.

  • Variant type because of its large memory footprint, will drag the running speed of the program, so we should try to use an explicit statement, specify the appropriate data type for the variable, but not too much use Variant type.
  • Note: Local variables only referenced in the process can only be used implicitly declared, public module-level, private module-level static variables and no implicit declarative way . The specific content of the variables mentioned in the role of the latter range.

2.3 forced variable declaration

Both methods can be mandatory explicit declaration of variables

1) writing a first row of a module Option Explicit statement.

2) VBE window " Tools " option " Require Variable Declaration

 

  • When set the "Require Variable Declaration", VBA will automatically generate the Option Explicit statement in a new module (including the worksheet modules, form modules, standard modules and class modules), but it does not automatically generate a module that already exists .
  • Option Explicit statement only play a role in it in the module !

Scope and lifetime of variables 3

3.1 Scope of variables

Range scope of a variable is a variable, while the variable declaration also defines the scope of the variable.

level

Keyword

Declaration position

Variable scope

Procedure-level variables

(Local variables)

Dim

Statement in the process

Only to access and change the variables in this process

Static

Statement in the course of a static variable

Private module-level variables

(Module level variable)

Private Dim

In the top of the module statement declared the area

In all processes in the module can access and change the variables

Public module-level variables

(A global variable)

Public

In the top of the module statement declared the area

Any process throughout the project in any module can access and change the variables

  • Dim statement at the module level and use the Private statement is the same. However, you can use the Private statement easier to read and interpret the code.

3.2 static variables

When using the Static statement to replace the Dim statement declares a variable that is called a static variable. Static variables are local variables can only be declared in the process.

Survival of 3.3 Variables

In addition to the range of variables, there are life cycle, its life cycle is consistent with its scope, the variable value will lose after the disappearance of the range.

Dim statement to declare a local variable, the variable value release memory after the end of the process, before you perform this procedure again, it will be re-initialized; static variables after the end of the process, as long as the entire program is still running, can retain its value not be re-initialized. And when all of the code to run is complete, a static variable will lose its range and survival.

Module-level variables and public level variables survival and survival of the same static variables, only after the end of the whole process will lose survival.

3.4 Variable references

1) Public module-level variables in different modules of the same name, when you call in the process, use

Module name variable name  reference.

2) a common module with module-level variables and procedure-level variables of the same name, used to call a public module-level variables in the process  module name variable name.  Reference; and call the procedure-level variables directly call the variable name.

3) other modules referring to the form module, the module worksheets and workbooks module level Public variables, using  the module name. Variable names  reference

  • Tip: View variable or procedure

When we want to see the process variables referenced or other processes, which module reference variable or process, or process variables can check this, right click and select "Definition", will automatically jump to the definition of this variable or procedure at this very convenient. Then right-click to select "Last Position" will return to where it left the cursor.

4 Summary

In fact, variables well understood, it imagine a container, the container shape is to determine the variable declaration and the capacity size, the variable name is affixed to the container by a special name, the data type is determined that the container is a gas or a liquid or solid loaded, variable value is what is put inside. So remember variable is occupied in memory storage location.

The focus of this lesson is that the scope and lifetime of variables variables. Scope determines the length of survival. Scope and lifetime of variables in a reference to the decision-making process variable values.

Excel Home Technology Forum

Excel home free online training center

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/11/07/2758673.html

Guess you like

Origin blog.csdn.net/weixin_33894640/article/details/93494878