Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
In the previous part of the lesson you saw how to use a Do - Loop statement to make instructions repeat. In order to stop the loop you tested a condition and used the Exit Do statement if the condition was met. This part of the lesson shows you an alternative way to stop the loop, using the Do Until statement.
You can click here to download the file for this page.
You can click here to download a file containing the sample code.
Extract and open the file linked to in the Files Needed section above. You'll find a version of the workbook you used in the previous part of this lesson:
Each worksheet contains a list of matches in a single group in the 2018 World Cup.
In the VBE you'll find a subroutine which generates a set of random scores for every match in every group:
The procedure uses two Do - Loop statements to complete its task.
Currently, the subroutine uses If statements to determine when to end each loop:
The two If statements shown here exit from the loops when the conditions are met.
When your loop includes a condition and an exit as either the first or last instruction within the loop, you can write the code in a more elegant way by including the logical test in the Do - Loop statement.
We'll start by altering the code for the nested Do - Loop statement. Select and delete the If statement which checks if the active cell is empty:
Select and delete the If statement highlighted here.
After removing the If statement, change the Do statement into a Do Until statement as shown below:
The Do Until statement allows you to include a logical test within the definition of the loop.
The logical test you include in the Do Until statement is evaluated each time the loop returns to the start:
The condition is checked each time the loop executes this line of code.
When the logical test returns True, the code will automatically exit from the loop and run the first instruction it finds below the Loop line. Run the subroutine to test that it works:
The nested loop with automatically stop when it reaches an empty cell.
Rather than testing the condition at the start of the loop, you can test it at the end instead. Try moving the condition from its current position:
Select and cut the highlighted piece of code.
Add the condition to the Loop line:
Paste the code at the end of the Loop line.
Run the subroutine to check that the code still works.
The only difference this makes is that the instructions inside the loop will also be executed at least once before the condition is evaluated. When the condition is part of the Do line, it is evaluated before the instructions are executed. This makes it possible for the instructions in the loop to never be carried out. For our example this doesn't affect the result as cell A3 always contains a value.
Do Until or Loop Until loops work when the condition you're using to determine when to exit the loop needs to be evaluated at the start or end of the loop. In the other Do - Loop statement in our code, the condition needs to be evaluated before the end of the loop:
In the outer loop, the condition we're testing to determine when to exit the loop is neither the first nor the last instruction within the loop.
In this case, using a Do Until or Loop Until statement means that that code wouldn't work properly:
Statement | Problem |
---|---|
Do Until | The loop would exit once it reaches the last worksheet but before it generates a set of scores for that worksheet. |
Loop Until | The code will generate a set of scores on the last worksheet and then cause a run-time error. This error will be caused by attempting to move to the next worksheet before testing if it is already on the last worksheet. |
To practise creating Do Until loops:
The workbook contains a list of all World Cup 2018 matches. When the button is clicked, we want to highlight every match played by the team selected in cell B2.
Pick any team from the list.
The drop down list was created using Excel's Data Validation feature. You can select cell B2 and choose Data | Data Validation from the ribbon to see how this is configured.
Sub Highlight_Matches()
'Clear colour from list
Range("A5:E52").Interior.ColorIndex = xlNone
Range("A5").Select
End Sub
Sub Highlight_Matches()
'Clear colour from list
Range("A5:E52").Interior.ColorIndex = xlNone
Range("A5").Select
Do Until ActiveCell.Value = ""
Loop
End Sub
Do Until ActiveCell.Value = ""
If ActiveCell.Offset(0, 1).Value = Range("B2").Value _
Or ActiveCell.Offset(0, 2).Value = Range("B2").Value Then
End If
Loop
Do Until ActiveCell.Value = ""
If ActiveCell.Offset(0, 1).Value = Range("B2").Value _
Or ActiveCell.Offset(0, 2).Value = Range("B2").Value Then
ActiveCell.Resize(1, 5).Interior.Color = rgbPaleGreen
End If
Loop
Do Until ActiveCell.Value = ""
If ActiveCell.Offset(0, 1).Value = Range("B2").Value _
Or ActiveCell.Offset(0, 2).Value = Range("B2").Value Then
ActiveCell.Resize(1, 5).Interior.Color = rgbPaleGreen
End If
ActiveCell.Offset(1, 0).Select
Loop
Do Until ActiveCell.Value = ""
If ActiveCell.Offset(0, 1).Value = Range("B2").Value _
Or ActiveCell.Offset(0, 2).Value = Range("B2").Value Then
ActiveCell.Resize(1, 5).Interior.Color = rgbPaleGreen
End If
ActiveCell.Offset(1, 0).Select
Loop
Range("B2").Select
Each match involving the selected team will be highlighted in the specified colour.
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.