Understanding the variable Declaration and assignment of VBA

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.