As we all know, if you learn English grammar and have a large vocabulary, you will basically master English.
Similarly, if you want to use vba, do as the Romans do and understand its composition. Simply put, vba contains data types.
Variables/constants, objects and commonly used sentence structures.
But it is much lower in quantity and complexity than English, and it is not so painful to remember words, so vba is actually very simple.
After getting familiar with the rules, the rest is to check the official functions and the operable objects provided by Excel.
By the way, functions are actually easy to understand and use. Get a function, such as Sum,
It is enough to know that it is the sum of multiple numbers, and the rest are used. For example, sum (1000,9) gives 1009.
One advantage of the function is that it hides the specific implementation details and provides concise usage.
Every cell in Excel is a data, whether it is a number, a letter or a punctuation mark.
Queue the data, eat the fruit, and throw different data into different baskets for classification. Baskets are data types.
In Excel-vba, there are only five data types: number, text, date, logic or error.
The first four are the most commonly used. See the following table for specific description:
Table 1. 1 VBA data type
In addition, an array is like a basket of fruit, in which multiple data can be stored.
It is not a specific data type, so it is more appropriate to call it a data structure.
The quantity that cannot be changed after definition is a constant; The opposite variable can modify the specific value.
In vba, to use a variable/constant, you must declare it first.
Constant declaration method is as follows:
Const constant name as data type = data stored in constant.
For example:
The variable declaration method is as follows:
Variable names must start with letters or Chinese characters, and cannot contain spaces, periods, exclamation points, etc.
The data type corresponding to the above table 1. 1.
More declaration methods differ from Dim declarations in scope:
When using arrays and objects, you should also declare them. The following is the declaration of the array:
In addition to using Dim to declare regular arrays, there are several ways to declare arrays:
Functions commonly used in arrays
functional description
UBound (array arr, [integer I]); & lt/br & gt;
UBound is the function name.
Arr and I are UBound parameters, and I in parentheses means I is not a necessary parameter.
Arr and Integer before arr and I indicate the data types of the corresponding parameters.