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 this lesson you saw how to use the Or operator to evaluate two conditions and return True when either condition was met. In this part of the lesson you'll see how to use the And operator to return True only when both conditions are met.
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 workbook linked to in the Files Needed section above. You'll find a modified version of an example you've seen previously:
You can enter the details of a film and then click the button to copy the values you have entered.
After clicking the button, we'd like to copy the film's details into one of three different worksheets according the score awarded:
Score | Worksheet |
---|---|
0 - 4 | Rubbish |
5 - 8 | OK |
9 - 10 | Great |
Each of these worksheets contains the column headings under which the data should be pasted:
The Great, OK and Rubbish worksheets contain the same column headings in the same cells.
You can find the subroutine which is triggered by clicking the button in the VBE:
So far, all the subroutine does is copy the details from the relevant cells.
To demonstrate how the And operator works, we'll write an If statement to move rubbish films to the Rubbish worksheet. In order for a film to be classed as rubbish, its score must be both greater than or equal to 0 and less than or equal to 4. Start by writing an If statement to test the first of these conditions:
Write the first condition as shown here but don't add Then to the end of the line yet.
You can add the second condition after adding the And operator:
Write the second condition and add Then to the end of the line.
If both conditions have been met, the entire logical test will return True. We'll use this to select the Rubbish worksheet:
Add a line to select the Rubbish worksheet and then end the If statement.
You can now test that the code works by entering a film with a score between 0 and 4:
Enter some film details and click the button.
After clicking the button you'll be taken to the Rubbish worksheet:
This is where you'll end up.
At this point you could add the code which pastes the copied data to the bottom of the list. Do this below the If - End If block:
Add this line below the End If.
Try clicking the button again and make sure that the details appear in the correct place on the Rubbish worksheet:
The new details will appear at the bottom of the table.
You can, of course use the And operator with ElseIf conditions. Let's add a set of conditions to deal with the OK films:
Add an ElseIf condition within the existing If - End If block.
Use the And operator to add a second condition to the ElseIf:
Add a second condition using the And operator.
Add an instruction to select the OK worksheet if both conditions are met:
Select the worksheet as shown here.
You can test that this part of the code works by entering the details for an average film:
Enter some film details and make sure the score is between 5 and 8.
Click the button and make sure that the film's details appear on the OK worksheet:
The details will appear at the end of the list.
To practise using the And operator:
If Range("B4").Value >= 0 And Range("B4").Value <= 4 Then
Worksheets("Rubbish").Select
ElseIf Range("B4").Value >= 5 And Range("B4").Value <= 8 Then
Worksheets("OK").Select
ElseIf Range("B4").Value >= 9 And Range("B4").Value <= 10 Then
End If
If Range("B4").Value >= 0 And Range("B4").Value <= 4 Then
Worksheets("Rubbish").Select
ElseIf Range("B4").Value >= 5 And Range("B4").Value <= 8 Then
Worksheets("OK").Select
ElseIf Range("B4").Value >= 9 And Range("B4").Value <= 10 Then
Worksheets("Great").Select
End If
Add details for any film.
The results should appear in the Great worksheet.
If Range("B4").Value >= 0 And Range("B4").Value <= 4 Then
Worksheets("Rubbish").Select
ElseIf Range("B4").Value >= 5 And Range("B4").Value <= 8 Then
Worksheets("OK").Select
ElseIf Range("B4").Value >= 9 And Range("B4").Value <= 10 Then
Worksheets("Great").Select
Else
Range("A5").Value = "Invalid Score!"
Exit Sub
End If
Perhaps we should have an extra condition which allows a negative score for any Twilight film.
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.