Collections and Loops
Exercise 6.02

Exercise 6.02

The aim of this exercise is to split a list of football matches in the 2018 FIFA World Cup into separate worksheets according to which group each match belongs to.

Files Needed

Click here to download the file needed for this exercise.

Instructions

  1. Extract and open the World Cup 2018.xlsm workbook and look at the Fixtures worksheet:
Fixtures

This worksheet contains a list of matches and the name of the group to which the match belongs.

  1. Look at the Groups worksheet:
Groups

This worksheet contains the names of the eight groups.

 
  1. In the VBE, insert a new module and create a subroutine called Create_Group_Sheets.
  2. Create a For Each loop which loops through the list of group names on the Groups worksheet.
  3. For each cell that the loop processes:
  • Add a worksheet to the Worksheets collection.
  • Change the name of the new worksheet to match the value of the cell that is being processed.
  • Move the new worksheet so that it becomes the right-most worksheet in the workbook.
  • Copy the column headings in cells A1:G1 of the Fixtures worksheet into the top row of the new worksheet.
  1. Run the subroutine and check that you have a new worksheet for each group:
New group sheets

Your workbook should now resemble this.

If you attempt to run the Create_Group_Sheets subroutine again it will cause a run-time error due to duplicating worksheet names.

  1. Create a new subroutine called Delete_Group_Sheets.
  2. Write a For Each loop which loops through the Worksheets collection of the workbook.
  3. Add an If statement within the loop which checks that the name of the worksheet being processed is not equal to "Fixtures" and is not equal to "Groups".
  4. Within the If statement, add an instruction to delete the worksheet currently being processed.

You can prevent a message appearing each time a worksheet is deleted by adding Application.DisplayAlerts = False before attempting to delete a sheet.

  1. Save the workbook and then run the Delete_Group_Sheets subroutine:
Deleted group sheets

After running the subroutine you should have only the Fixtures and Groups worksheets in the workbook.

 
  1. Create a new subroutine called Split_Fixture_List.
  2. Add instructions to call the Delete_Group_Sheets followed by the Create_Group_Sheets subroutines.
  3. Create a For Each loop which loops through cells A2:A49 on the Fixtures worksheet.
  4. For each cell that is processed by the loop:
    1. Copy the row of data to which the cell belongs.
    2. Paste the copied data at the bottom of the list on the worksheet whose name matches the value of the cell being processed.
  5. Run the subroutine and check that each worksheet receives the correct list of fixtures:
Results

Each of the group worksheets should contain six fixtures.

  1. Create a new subroutine called Change_Column_Widths.
  2. Write a For Each loop which processed the Worksheets collection in the workbook.
  3. Add an If statement within the loop which checks that the name of the worksheet being processed is not equal to "Fixtures" and is not equal to "Groups".
  4. Within the If statement, add an instruction which applies the AutoFit method to the columns of the entire region of data beginning in cell A1 on the worksheet being processed.
  5. At the bottom of the Split_Fixture_List subroutine, add an instruction which calls the Change_Column_Widths procedure.
  6. Run the Split_Fixture_List subroutine and check the results in Excel:
Results

The new results should be more readable than the previous ones.

  1. Save and close the workbook.

Answer Files

Click here to download a file containing a suggested answer.

This page has 0 threads Add post