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
551 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 ...
So far in this lesson, you've learnt how to perform a set of instructions when a condition you've tested for returns True. In practice, you'll often want to perform a different set of instructions when your condition returns False. This part of the lesson explains how to use the Else clause of the If statement to do exactly that.
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 worksheet which contains a very simple game based on generating random numbers:
You can click the button in cell C2 to create a new set of random numbers.
Clicking the button in cell C2 runs a subroutine which creates a new set of random numbers:
You can find the subroutine in the VBE.
If we assume that the player with the highest number is the winner, we'd like to add some code which will indicate who has won and lost:
We'll use different colours and text to indicate the winner and loser.
To begin, we'll add some code which changes the fill colour of the two cells if player 1 is the winner. Add an If statement below the existing code in the Roll_Again subroutine:
Test if the value of cell B2 is greater than that of cell D2. You can use the > operator to do this.
The > (greater than) symbol is an example of a comparison operator. You can see a list of other comparison operators in the Reference section of this module.
Add instructions to change the fill colour of the two cells, as shown below:
Here we're using green for the winner and red for the loser. Feel free to use any combination of colours you prefer.
Add the End If statement:
As this is a block If statement we'll need an End If statement.
In Excel, try clicking the Play! button until cell B2 contains a larger number than D2:
When player 1 wins, the cells will change colour.
Unfortunately for player 2 at this stage, there is no way for them to win:
When cell B2 doesn't contain a higher number than D2, the cells remain grey in colour.
To allow player 2 to win, add an Else clause to the If statement:
The Else keyword should occupy its own, separate line of code between the If and End If statements. Conventionally, it should be placed at the same indent level as the If statement.
You can now add instructions below the Else clause:
These instructions will flip the red and green colours when player 2 wins.
Return to Excel and try playing the game again:
This time, player 2 can win!
Player 2 has a slightly higher chance of winning than player 1 because we haven't added any code to handle a draw. Player 1 will win only if the value in B2 is greater than D2. If this is not the case, player 2 will win automatically.
Now that you've seen the basic concept of the Else clause you can either complete the Extra Practice section below, or move on to the next part of the lesson to learn about the ElseIf statement.
To practise using the Else clause:
If Range("B2").Value > Range("D2").Value Then
Range("B2").Interior.Color = rgbGreen
Range("D2").Interior.Color = rgbRed
Range("B3").Value = "WINNER!"
Range("D3").Value = "LOSER!"
Else
Range("B2").Interior.Color = rgbRed
Range("D2").Interior.Color = rgbGreen
End If
If Range("B2").Value > Range("D2").Value Then
Range("B2").Interior.Color = rgbGreen
Range("D2").Interior.Color = rgbRed
Range("B3").Value = "WINNER!"
Range("D3").Value = "LOSER!"
Else
Range("B2").Interior.Color = rgbRed
Range("D2").Interior.Color = rgbGreen
Range("B3").Value = "LOSER!"
Range("D3").Value = "WINNER!"
End If
The appropriate text will appear below each player's number.
If Range("B2").Value > Range("D2").Value Then
Range("B2").Interior.Color = rgbGreen
Range("D2").Interior.Color = rgbRed
Range("B3").Value = "WINNER!"
Range("D3").Value = "LOSER!"
Range("B3").Font.Color = rgbGreen
Range("D3").Font.Color = rgbRed
Else
Range("B2").Interior.Color = rgbRed
Range("D2").Interior.Color = rgbGreen
Range("B3").Value = "LOSER!"
Range("D3").Value = "WINNER!"
End If
If Range("B2").Value > Range("D2").Value Then
Range("B2").Interior.Color = rgbGreen
Range("D2").Interior.Color = rgbRed
Range("B3").Value = "WINNER!"
Range("D3").Value = "LOSER!"
Range("B3").Font.Color = rgbGreen
Range("D3").Font.Color = rgbRed
Else
Range("B2").Interior.Color = rgbRed
Range("D2").Interior.Color = rgbGreen
Range("B3").Value = "LOSER!"
Range("D3").Value = "WINNER!"
Range("B3").Font.Color = rgbRed
Range("D3").Font.Color = rgbGreen
End If
Everything should now work as expected.
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.