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 ...
Software ==> | Excel VBA Macros (52 exercises) |
Topic ==> | Looping over collections (11 exercises) |
Level ==> | Average difficulty |
Subject ==> | VBA training |
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.
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:
Your macro should 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:
Range("A1048576").End(xlUp).Offset(1, 0).Select
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.
You can find other training resources for the subject of this exercise here:
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.