当前位置:网站首页>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 .
边栏推荐
- Game business DDoS attack and defense confrontation case sharing
- ClassNotFoundException v/s NoClassDefFoundError
- Coding enhances security vulnerability scanning capability and helps the team "move left safely"
- Robot toolbox matlab robotics toolbox
- Learn typescript with VAM (phase 1)
- How to save data to the greatest extent after deleting LV by misoperation under AIX?
- Low education without food? As an old Android rookie in the past six years, I was the most difficult one
- A comprehensive understanding of fiber to home FTTH and optical splitter
- [2021 taac & Ti-One] FAQs related to preliminary round computing resources
- Cloud development environment to create a five-star development experience
猜你喜欢
![[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

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)

Why do you develop middleware when you are young? "You can choose your own way"
随机推荐
Explore cloudera manager management software tuning (1)
TCB series learning articles - using redis extension in cloud functions
Following the previous SYSTEMd pit
FPGA systematic learning notes serialization_ Day9 [serial port printing of PS terminal of Xilinx zynq7000 series]
Live broadcast Preview - on April 1, I made an appointment with you to explore tcapulusdb with Tencent cloud
[play Tencent cloud] experience and development of game multimedia engine (II)
Snapshot management for elastic cloud enterprise
In those years, I insisted on learning the motivation of programming
API documents are simple and beautiful. It only needs three steps to open
Robot toolbox matlab robotics toolbox
Edit distance (linear dp+ violence matching)
Five steps to effectively monitor network traffic
Sigai intelligent container damage identification products are deployed in Rizhao Port and Yingkou Port
Go path customized project path package dependency
未来银行需要用明天的思维,来思考今天架构
C4D learning notes
Cloud native monitoring via blackbox_ Exporter monitoring website
[tke] nodelocaldnschache is used in IPVS forwarding mode
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)
One article combs multi task learning (mmoe/ple/dupn/essm, etc.)