WISE OWL EXERCISES
EXCEL VBA MACROS EXERCISES
Excel VBA Macros | Looping over collections exercise | Split list of dinosaurs onto separate sheets
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 the course 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.
Open the file called Dino List.xlsx from the folder shown above.
Your task is to split the main list of dinosaurs into three separate sheets based on the value of the Diet column:
You need to copy rows from the Dino List sheet into the appropriate worksheet.
Start by writing a For Each loop to loop over the list of dinosaur names in column A of the Dino List sheet. You'll need at least one Range variable to do this.
Declare a String variable and add a line inside the For Each loop which captures the diet of the current dinosaur. You can Offset from the cell that is referenced by the Range variable to do this.
Copy the current dinosaur's data into the appropriate worksheet. You can do this in a variety of ways but one simple approach would be to:
- Copy the current row of data (use the Range variable rather than the ActiveCell).
- Select the worksheet whose name matches the dinosaur's diet.
- Find the next blank row in the worksheet, for example:
- Paste the copied data at this cell.
Add a new line after the For Each loop which copies the column headings from the Dino List sheet.
Declare a Worksheet variable and add a new For Each loop at the bottom of the procedure which loops over the Worksheets collection.
Inside the new loop, add code which pastes the copied column headings into the first row of each worksheet except for the Dino List sheet.
Include a line inside the loop which changes the width of the columns to fit the data they contain.
This is what you should see on the Omnivore sheet.
As a final flourish, you could add a separate subroutine which clears the three diet worksheets and call this at the start of your main procedure.
Save and close the workbook.