Previous: [Python] Arts students 0 BASIC programming Series one--basic definition of objects, collections, properties, methods
Next: [Python] Liberal arts students 0 Basic programming series three--Classification of operators
(The syntax is different from Python, but the concepts and logic need to be understood), as an example of the most basic Excel VBA
3.1 Data:
In Excel, what you need to process and calculate is the data in Excel.
3.2 Data type:
collectively , the same type of data .
Depending on the nature of the data, it can be divided into different categories. Example: text, date, value, etc.
For different categories of data types , the computer stores it in different ways .
3.2.1 Data type: (VBA for Excel example)
1, byte type byte 1 byte save 0~255 Integer
2, integers integer 2 bytes save -32768~32767 integer
3, Long integer type long 4 bytes save -2147483648~2147483647 integer
4, single-precision floating-point 4 bytes
5, double-precision floating-point double 8 bytes
6. Currency type Currency 8 bytes
7, Decimal type decimal 14 bytes
8. String 10 bytes + String length/String length
9, Date-type Date 8 bytes
10, bool Boolean 2 bytes, save the result of logical judgment: TRUE or False
11. Variable Object 4 bytes
12. Variant Variant 16 bytes/22 bytes + string length
13. User-defined Type
3.2.2 Data type: (Python for example)
1, Number: integer int, floating-point float, boolean bool, complex complex
2, strings string
3. List
4, tuple tuple
5, Set sets
6. Dictionary Dictionary
※ Save containers of different data types :
In the face of different data types, when programming, you should tell the program what type of "container" is used to store the data to be processed .
constants and variables are the two "containers" that hold data.
No matter what type of data is stored, variables can be replaced, reused, and constants are not allowed.
3.3 Variables:
Variables are like containers,
Used to save data or objects that need to be temporarily saved while the program is running;
The values stored in the variable can be changed at any time.
3.3.1 Declaring variables: (defined under variables)
To save the data in a variable, you first need to declare the variables first.
Declaring variables: Use the VBA statement in Excel to specify the name of the variable and the type of data that can be stored.
Grammar:
Dim variable name as data type
Dim Str as String
Explain:
statement declares a variable of type string, named Str.
After declaring a variable, you can store the text string in str, but you cannot store the date, numeric value, or other type of data.
Other VBA syntax in Excel:
Dim variable name as data type → declare local variable
Private variable name as data type → declare module-level variable
Public variable name as data type → declare common variables
Static variable name as data type → declare static variable
Scope of the 3.3.2 variable: (the range used by the variable)
Determines which module or procedure the variable can be used in.
Local variable: scope individual procedure.
In a process of a program, the dim or static statement declares that the procedure for declaring a variable can be used.
Module-level variables: scoped individual modules.
Before the first procedure of a module, the dim or private statement declares that all procedures in the module are available.
Public variables: scope all modules.
Before the first procedure of a module, the public statement declares that all the modules in the project can be used.
3.3.3 Assigning values to variables: (storing data in variables)
3.3.3.1 assign values to variables of data types such as text, date, numeric, and so on.
Syntax: [let] variable name = data
Example: let str = "I love Excel" or str = "I love Excel"
3.3.3.2 assigning values to objects of type (object) variables such as worksheets, cells, etc.
Syntax: Set variable name = Object
Example: Set Rng = Worksheets (1). Rang ("A1")
3.4 Constants:
Constants are often used to store fixed values that are not modified, such as PI, personal income tax, and so on.
3.4.1 Declaration constants:
Specifies the name and data type of the constant, and assigns a value to the constant, and the value after the assignment cannot be re-assigned.
Syntax: Const variable name as data type = numeric
Example: Const p as single = 3.14
Scope of the 3.4.2 constant: (The range used by the constant)
Is the same as the scope of the variable.
Previous: [Python] Arts students 0 BASIC programming Series one--basic definition of objects, collections, properties, methods
Next: [Python] Liberal arts students 0 Basic programming series three--Classification of operators
[Python] Arts students 0 BASIC programming series two--basic concepts of data types, variables, constants