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.

Software ==> Advanced VBA  (33 exercises)
Version ==> Any MS Office versions
Topic ==> Working with shapes  (3 exercises)
Level ==> Harder than average
Subject ==> VBA training
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

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 lined up

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:

Car.Fill.UserPicture "D:\exercises\car1.jpg"

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:

Progress message

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:

Results of 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.

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post