Do Until loop is very similar to the Do While, the difference is that the Do Until will work as long as the condition is met = "" Do Until and as long as the condition is met, in the previous example as long as the content of the cells was different from the 'empty' <> "".
Syntax:
Do Until [condition]
[Instructions]
Loop
[Instructions]
Loop
Example 1
I provide the code that does exactly the same as the macro described in the Do While Loop lesson, but uses the Do Until Loop.
Sub Loop_Do_Until ()
Do Until ActiveCell.Value = "" 'loop condition as long as you do encounter a blank cell
If Selection.Interior.ColorIndex = 3 Then 'If readjust conditional: if the cell is red
ActiveCell.Offset (0, 1). Range ("A1"). Select 'Move one cell to the right
ActiveCell.Value = "selected" 'active cell type in the text of the "chosen"
ActiveCell.Offset (0, -1). Range ("A1"). Select 'back one cell to the left
End If 'If the end of a conditional statement
ActiveCell.Offset (1, 0). Range ("A1"). Select 'Move one cell down
Loop 'end of the loop back to the line of Do Until
End Sub
Do Until ActiveCell.Value = "" 'loop condition as long as you do encounter a blank cell
If Selection.Interior.ColorIndex = 3 Then 'If readjust conditional: if the cell is red
ActiveCell.Offset (0, 1). Range ("A1"). Select 'Move one cell to the right
ActiveCell.Value = "selected" 'active cell type in the text of the "chosen"
ActiveCell.Offset (0, -1). Range ("A1"). Select 'back one cell to the left
End If 'If the end of a conditional statement
ActiveCell.Offset (1, 0). Range ("A1"). Select 'Move one cell down
Loop 'end of the loop back to the line of Do Until
End Sub
Example 2
In this example, I would like to present to the user Exit, which allows you to exit the loop.
In the table in columns F and G, our investor would have to stop updating the table and introduced in the cell text "still does not."
Exit For is often combined with IF, to exit the loop takes place after fulfillment of the assumed condition.
Sub Loop_Do_Until_From_instruction_Exit_Do ()
Do Until ActiveCell.Value = "" 'loop condition as long as you do encounter a blank cell
If Selection.Interior.ColorIndex = 3 Then 'If readjust conditional: if the cell is red
ActiveCell.Offset (0, 1). Range ("A1"). Select 'Move one cell to the right
ActiveCell.Value = "selected" 'active cell type in the text of the "chosen"
ActiveCell.Offset (0, -1). Range ("A1"). Select 'back one cell to the left
End If 'If the end of a conditional statement
ActiveCell.Offset (1, 0). Range ("A1"). Select 'Move one cell down
If ActiveCell.Value = "no further" Then 'If the cell contains "more than" time
Exit For 'exit loop
End If 'If the end condition
Loop 'end of the loop back to the line of Do Until
End Sub
Do Until ActiveCell.Value = "" 'loop condition as long as you do encounter a blank cell
If Selection.Interior.ColorIndex = 3 Then 'If readjust conditional: if the cell is red
ActiveCell.Offset (0, 1). Range ("A1"). Select 'Move one cell to the right
ActiveCell.Value = "selected" 'active cell type in the text of the "chosen"
ActiveCell.Offset (0, -1). Range ("A1"). Select 'back one cell to the left
End If 'If the end of a conditional statement
ActiveCell.Offset (1, 0). Range ("A1"). Select 'Move one cell down
If ActiveCell.Value = "no further" Then 'If the cell contains "more than" time
Exit For 'exit loop
End If 'If the end condition
Loop 'end of the loop back to the line of Do Until
End Sub
0 coms:
Post a Comment
Want something to write? You don't have to log in.