ADVANCED VBA EXERCISES▼
Advanced VBA | Working with shapes exercise | Use shapes to create a simulation of a Formula One race
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
You can learn how to do this exercise if you attend one of more of the courses listed below!
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
It's easiest to do this exercise in two parts: first draw the starting grid, then run the race.
To start the race, first create a new workbook, and write a macro (using the images in the above folder) to draw autoshapes at the start of the first 4 rows of a spreadsheet:
The cars are 40 points wide, 30 points high and begin 20 points in from the left of their cells and 10 points down from the top. The ColumnWidth of each column is 15, and the RowHeight of each row is 50.
The easiest way to draw the cars is to draw a basic rectangular autoshape (type 1), then run a line of code like this to apply a picture to it:
When you've got the grid set up, it's time to run the race! Write a second macro to loop until a car has won (ie reached column H). For each time round the loop, you could display a message box to enable you to see what's going on:
Clicking on the message box should run the next move, which should advance one of the four cars (chosen at random) one cell. The answer uses the expression Int(Math.Rnd() * 4) + 1 to choose a random car number.
Here's what your screen might look like after running a race:
Car 4 has won, and you should report this fact with a message displayed on screen!
When you've got this working, save this workbook as Chequered flag, then close it down.