Today, the author would share something new about the VBA program structure In this article. It’s also a very important part of VBA.
The concepts of variables, data types, and operators learned in the previous articles are the “static” part of VBA. In contrast, the program structure is the “dynamic” part of VBA. After learning the program structure, you can really write a VBA program that can be run.
Let’ look at an example program structure below.
After the above code is run. The program will determine whether each cell is empty in the A2:A10 cell range in turn. If it is empty, it will be filled with the value of the previous cell.
Now we will decompose the above code into several parts to explain in detail.
Sub Statement
You can think of these two lines of code as the head and tail of the program structure. This is actually the smallest structure in which the program actually runs. Code steps need to be written in this process. So that the code can run.
Dim statement
A statement is a line of code that represents a complete meaning.
For example, in the first line of the example, declaring a variable is a statement. It means that an integer variable is declared. In the same way, the second line, the third line, and every other line are sentences. The process in VBA is composed of such a sentence.
Program Structure
Most programming languages have three basic program running structures: sequence structure, loop structure, and judgment structure. Various simple to complex algorithms is completed by combining these three basic structures.
- Sequence Structure
The order in BA is from top to bottom and from left to right.
- Loop Structure
When using the loop structure, loop part of the code, in accordance with the specified number of loops, the loop is repeated. In our example, the code between For and Next i is a loop code.
In VBA, there are many loop structures, in this case, it is a For loop structure. In the For loop structure, the first line specifies the number of loops, and the last line indicates the start of the next loop.
- Judgment Structure
Simply put, in this structure, when the provided expression is True, the main part of the judgment structure will be executed, otherwise, it will be skipped.
In the sample code, the beginning of If and the end of End If are typical judgment structures. In the first line, it is judged whether the isBlank variable is true. If it is, the main part of the judgment structure is executed, otherwise skipped.
Comment
Usually, after a piece of code is written, it will not be all right. It may need to be modified during actual use to meet the latest requirements. When you open it for viewing after a while, you may have forgotten the idea at that time, and you can’t quickly remember the actual purpose of some code, let alone let others view it. At this time, you need to comment out.
Comments are an explanation of the code and do not affect the operation of the code. The comment statement in VBA starts with a single quotation mark (‘), followed by the content that needs explanation.
Summary
Now you have learned the basic VBA program structure. The author suggests that you open an Excel workbook to try when you reading this Tutorial.
Leave a Reply