vba’s while loop

Dowhileloop Usage in VBA

DowhileLoop usage in VBA is as follows:

If you don’t know how many times to repeat something in VBA, use the Do…. .Loop statement.

The Do…. .Loop statement repeats a piece of code until the condition is true or the condition becomes true.

Repeat the code until the condition is true

Use the While keyword to check the Do…. .Loop statement’s condition.

Do While i>10

‘some code

Loop

If i is equal to 9, the code inside the above loop terminates.

Do

‘some code

Loop While i>10

The code inside this loop will be executed at least once, even if i is less than 10.

Repeat the execution of the code until the condition becomes true

Use the Until keyword to check that the Do…. Loop statement’s condition.

Do Until i=10

‘some code

Loop

If i is equal to 10, the code inside the above loop terminates.

Do

‘some code

Loop Until i=10

The code inside this loop will be executed at least once, even if i is equal to 10.

Exit Do…Loop

The code inside this loop will be executed at least once, even if i is equal to 10.

Exit Do…. .Loop

You can exit the Do… via the ExitDo keyword. .Loop statement.

Do Until i=10

i=i-1

If i<10 Then Exit Do

Loop

The code inside this loop is executed whenever i is not 10 and i is greater than 10.

Extension:

Use of while in VBA

WhileI<=3

MsgboxI& “If it is equal to 0 here, then a statement must precede WhileI<=3: I=1”

MsgboxSheet1.Name’If an error is reported here, then it proves that the Sheet1 the worksheet does not exist.

ifi<1Theni=1’You can also add a statement here to prevent errors, preventing i from being less than 1.

id=Sheet1.Cells(I,1)

You can test this by adding the above two lines of code inside the code.

An initial suspicion is that the code is generating the error because the variable i is not initialized before the code WhileI<=3. Once in the loop body there is what is referred to as an “application definition or object definition error.”

Difference between five kinds of loop statements in vba

1, For……Next statement, applicable to sequential operations

Example 1. Output 1 to 100 in the immediate window

Forn=1to100 ‘From 1 to 100, execute the next line of code

debug.Printn

nextn

2.For……Each……Next statement, applicable to the operation of the collection

Example 2.Output the specified cell area all the The contents of the cell

ForEachCellinmyRange ‘traverse all the cells in the region

debug.PrintCell.Value

nextCell

3, DoWhile condition … …Loop statement.

Example 3, Output 1 to 100 in Immediate Window

n=0

DoWhilen<=100′ When n is less than or equal to 100, execute the next line of code, otherwise exit

n=n1

Debug.Printn

Loop

4, While condition ……Wend statement.

Example 4, Output 1 to 100 in the immediate window

n=0

Whilen<=100′ When n is less than or equal to 100, execute the next line of code, otherwise exit

n=n1

Debug.Printn

Wend

5. Do ……LoopUntil conditional statement.

Example 5, Output 1 to 100 in the immediate window

n=0

Do ‘Execute the next line of code

n=n1

Debug.Printn

LoopUntiln>100 ‘Exit the loop when n is greater than 100

6, Do……LoopWhile statement, the ghost of the old thinking is really the most bizarre, put While in the Loop after the conditions are not the exit loop, but continue to execute the conditions of the loop. Really make Chinese thinking puzzling.

Example 6, in the immediate window output 1 to 100

n=0

Do ‘Execute the next line of code

n=n1

Debug.Printn

LoopWhilen<=100 ‘When n is less than or equal to 100. Continue executing the loop.

vbadowhile dead loop problem\

The following code output is normal, immediate window is not executable

PrivateSubForm_Load()

Form1.AutoRedraw=True

Counter=0

Do< /p>

Counter=Counter+1

LoopWhileCounter<10

PrintCounter

EndSub

======================================= ===========

Because variable values are not recorded in the immediate window