当前位置:网站首页>VBA Daniel used the nested loop
VBA Daniel used the nested loop
2022-06-24 17:17:00 【Monkey data analysis】
This is a free tutorial 《Excel VBA: Office automation 》 Of the 11 section , Introduce nested loop structure .
2. These are mastered , You dare to say that you understand VBA
3.VBA Variable 5 Summary of the year's pits
4.VBA The most important mandatory statement in English is , Who can see who can understand
5.VBA Master the loop structure , You can improve your efficiency 500 times
6. Master VBA Branching structure , Write less programs 100 That's ok
7.VBA Application error , With three magic weapons of debugging ,bug There is no the
8.VBA String Introduction , This is enough
9. And write your own code ?VBA Record macro to learn about
10.VBA The circular structure is not good , Let's have another one
Read the previous series VBA Content , I think everyone must have found a problem : All the previous actual cases are handled 「 One column, many lines 」 problem , But what we often encounter in practical work is 「 Multiple rows and columns 」 problem , How to deal with this problem ?
What I want to say is , This section of nested loop sharing is devoted to this problem .
1. What is loop nesting ?
So-called 「 A nested loop 」 Is to share what we shared earlier Branching structure 、 Loop structure And so on , Then complete complex tasks that are difficult for a single knowledge point to complete alone .
The popular explanation is :IF loop and WHILE Loop everyone together . You have me , I have you. , Realize the actual needs of the work .
2. Loop nested case practice
Case study : Will make use of 「1- Macro program 」 according to 「2- Hierarchy 」 The rules of , Yes 「3- Data area 」 Data processing , formation 「4- Output results 」.
First , We make a preliminary analysis of the needs of this case :
“ utilize 「1- Macro program 」” Is to draw a rounded rectangle with a high color value , And then specify what we're going to write Macro program .
“ according to 「2- Hierarchy 」 The rules of ” According to the hierarchical structure conditions on the right , utilize IF Branching structure Judge the specified conditions .
“ Yes 「3- Data area 」 Data processing ” Is to choose what you are good at Loop structure , Cycle through the specified data areas one by one .
“ formation 「4- Output results 」” It is to output the results in the specified format at the specified location .
After analyzing the above requirements , We basically have a clear idea , Then move on .
Same as before , First , We show the code that has implemented the case function ; then , Explain the main structure of this code ; Last , Simulate the operation of the code , In order to deepen our understanding .
Through the code shown above , We can see that the core architecture of the whole code adopts 3 Hierarchical nested structure , Respectively :
The first 1 Layer of Do...While Loop structure , It is mainly used to control tables 「 That's ok 」 A cycle of directions ;
The first 2 Layer of For Loop structure , It is mainly used to control tables 「 Column 」 A cycle of directions ;
The first 3 Layer of IF Branching structure , It is mainly used to judge the specified conditions .
adopt 3 The loop nesting structure formed by the combination of , Finally, the more complex of the above cases was completed 「 Multiple rows and columns 」 demand .
According to the code above , Let's simulate the running process of this little program
1) Before program reading 5 That's ok
The program uses 「 Compulsory declaration 」, Defined variables i And variables j, And the variable i Assign initial value as 3, And assignment variables i by 3 The main reason is : Because of the variable i control 「 That's ok 」 A cycle of directions , The data area is judged from the 3 The line .
2) Program execution Do...While The loop body
Program read 「Do While Cells(i, 2) <> ""」 Indicates that the program has entered the 1 Layer of 「 Row cycle 」, In other words : Cell B3 Whether the value of is empty ? We found by looking at the left 「 Cell B3=738」, Not empty . then , The program goes to the next line to execute .
3) Program execution For Loop structure
Program read 「For j = 2 To 7」 Indicates that the program has entered the 2 Layer of 「 Column loop 」, In other words :j The value is 2 To 7 Between , Every cycle j The value of is increased 1 , until j=7 When the whole For The loop ends .
See here , Maybe you have some questions , Why variable i To limit its value range ?
Everyone must have their own answers in their hearts : The reason why the variable j The value of is limited to 2-7 Between , Because the data areas we need to judge are mainly distributed in B Column -G Column , Their corresponding cells The numerical value is 2-7.
4) Program execution IF Branching structure
At this point, the program enters If Branching structure , From the above, we can know that at this time :i = 3,j = 2, namely :「Cells(2, 3)」 Express 「 Cell B3」, By looking at the data on the left, you can see 「 Cell B3 = 738」, It is located in 「 Rich peasants 」 Range interval , Then it is obvious that only the second branch structure meets the above conditions , namely : The program executes automatically 「ElseIf Cells(i, j) >= 500 And Cells(i, j) < 700 Then」 and 「Cells(i, j) = Cells(i, j) & " | Rich peasants "」.
And because according to the rules , Cells that meet the criteria , Will be in the specified format ——「 income | Grade 」 Write results , such as :「738 | Local tyrants 」. therefore , Will appear 「Cells(i, j) = Cells(i, j) & " | Rich peasants "」 This code .
then , Wait until variable j After the execution, it is 2-7 After all values of the interval , Original table No 3 All the data of the row will be converted , Here's the picture :
In other words, it means : When 「i = 3」 when ,For The branch structure should be circular 1 Wheel variable j, For the original table, it completes the 3 Conversion of row data .
5) Program read 「i = i + 1」 and 「Loop」
When the program is executed to 「i = i + 1」 When , Indicates the last round of variables i The loop execution of has been completed ; And the program runs to 「Loop」 when , Indicates that the program is ready to execute the next round .
When the program conforms to 「Do While Cells(i, 2) <> ""」 This condition is , The program will proceed to the next round . Once the above conditions are not met , Then the whole program will jump out 「Do...While」 loop .
When 「i = 15」 when , here 「Cells(i, 2)」 Express 「 Cell B15」, Looking at the table on the left, you can find , Cell is empty , disqualification , Because there is no other code below , Get into 「End Sub」 sentence , End directly .
3. summary
Loop nesting is the branching structure we shared earlier 、 Loop structure, etc , Then complete complex tasks that are difficult for a single knowledge point to complete alone .
From the above, we can find that : Loop nesting can be compared to Lego building blocks , Use different building blocks to combine different things . As for what to combine , It depends on your thinking 、 practice , Think again and practice again , So again and again .
We found that , In fact, many things in the world are composed of different small modules . So is knowledge , Only the continuous accumulation 、 Constant practice , And the rest will be left to time .
边栏推荐
- [2021 taac & Ti-One] frequently asked questions related to Ti-One products
- A solution for building live video based on open source real-time audio and video webrtc architecture
- Is CICC securities reliable? Is it legal? Is it safe to open a stock account?
- IBM:以现代化架构支撑AI与多云时代的企业数字化重塑
- A comprehensive understanding of fiber to home FTTH and optical splitter
- [log service CLS] Tencent cloud game battle engine mgobe accesses CLS
- zblog系统如何根据用户ID获取用户相关信息的教程
- zblog判断某个插件是否安装启用的内置函数代码
- To redefine the storage architecture, Huawei has used more than five "cores"
- When the game meets NFT, is it "chicken ribs" or "chicken legs"?
猜你喜欢
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)

MySQL learning -- table structure of SQL test questions

Why do you develop middleware when you are young? "You can choose your own way"

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)
随机推荐
实现TypeScript运行时类型检查
[play with Tencent cloud] check 9 popular Tencent cloud products
Markdown syntax -- Formula
Bypass kernel function pointer integrity check
04. Tencent cloud IOT device side learning - network connection and device authentication
[tke] nodelocaldnschache is used in IPVS forwarding mode
Solution to the problem that qlineedit setting qdoublevalidator setting range is invalid
How to customize the log output format of zap?
When the game meets NFT, is it "chicken ribs" or "chicken legs"?
IBM: supporting AI and enterprise digital reshaping in the cloud era with modern architecture
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)
About with admin option and with grant option
Tencent monthly security report helps rural revitalization, releases cloud security reports, and jointly builds a joint network security laboratory
NFT元宇宙源码搭建解析与介绍
How to collect and define project requirements in the early stage of EDI project implementation?
Introduction to website development for zero foundation Xiaobai
In those years, I insisted on learning the motivation of programming
"Competition" and "opportunity" hidden in security operation in the cloud Era
Zblog system realizes the tutorial of the number of articles published on the same day when the foreground calls
Ramda 鲜为人知的一面