当前位置:网站首页>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 .
边栏推荐
- Yuanqi forest started from 0 sugar and fell at 0 sugar
- liver failure! My friend made a programming navigation website!
- Yupi made an AI programming nickname generator!
- Building a cross public chain platform to solve DAPP development problems
- 中金证券靠谱吗?是否合法?开股票账户安全吗?
- Quick view of product trends in February 2021
- 跟着Vam一起学习Typescript(第一期)
- How to convert XML to HL7
- Best practices for H5 page adaptation and wechat default font size
- 05. Tencent cloud IOT device side learning -- mqtt protocol client implementation
猜你喜欢
![[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)
随机推荐
liver failure! My friend made a programming navigation website!
[play with Tencent cloud] & lt; trtc-room&gt; Applet component usage
Future banks need to think about today's structure with tomorrow's thinking
Jmeter+grafana+influxdb build a visual performance test monitoring platform
Low education without food? As an old Android rookie in the past six years, I was the most difficult one
主链系统发展解析
究竟有哪些劵商推荐?现在网上开户安全么?
When the game meets NFT, is it "chicken ribs" or "chicken legs"?
"Competition" and "opportunity" hidden in security operation in the cloud Era
Tencent monthly security report helps rural revitalization, releases cloud security reports, and jointly builds a joint network security laboratory
How to collect and define project requirements in the early stage of EDI project implementation?
让UPS“印象派用户”重新认识可靠性
As for IOT safety, 20 CSOs from major manufacturers say
Issue 003 how to detect whether a sticky positioned element is in a pinned state
TRCT test cloud + article online speed
IBM:以现代化架构支撑AI与多云时代的企业数字化重塑
The RTSP video image intelligent analysis platform easynvr cascades to the superior platform through the national standard for playback optimization
[playing with Tencent cloud] a solution to the impassability of cross-border access to foreign websites using Tencent cloud CVM
Elastic searchable snapshot function (frozen Tier 3)
The problem is as big as the middle stage