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 ...
In the previous part of the lesson you saw how to refer to an object in a collection using the object's name. When you don't know the name of an object, you can instead refer to it using its index number, as this part of the lesson shows you.
You can click here to download the file for this part of the lesson.
You can click here to download a file containing the sample code.
Download and extract the workbook linked to in the Files Needed section above. You'll find a copy of the workbook you used in the previous part of the lesson containing a number of worksheets, charts and tables.
The sample workbook you have opened contains three objects in the Worksheets collection and two objects in the Charts collection:
Altogether, there are five objects in the Sheets collection.
You can reference an object in any of these three collections using the index number of the object. Objects in these collections are numbered based on their position from left to right. We can refer to the NFC 2017 worksheet as worksheet number 3. Open the VBE and find the subroutine called ReferToObjectByNumber:
The subroutine contains a single instruction to activate the workbook that contains the code.
Add an instruction to the subroutine which selects the third worksheet:
Refer to the Worksheets collection and enter the number of the object in parentheses. You can then enter a full stop and apply the Select method.
Run the subroutine and check the result in Excel:
You should find that the NFC 2017 worksheet has been selected.
Edit the code to apply the Select method to the third object in the Sheets collection:
Change the reference to the Worksheets collection to Sheets.
Run the code again and check the result in Excel:
This time the AFC Chart will be selected as it is the third item in the Sheets collection.
Alter the code to select the second object in the Charts collection:
Change the Sheets collection to the Charts collection and refer to item number 2.
Run the subroutine and check the results in Excel:
The second item in the Charts collection is also the fifth item in the Sheets collection.
Excel VBA collections are indexed starting from 1. Some other VBA libraries index their collections starting from 0.
Using the index number of a sheet is useful when you want to rearrange the order of sheets and you can't rely on the sheet names. Add a new subroutine called ArrangeSheets and add an instruction to activate ThisWorkbook:
Create a basic subroutine as shown here.
Add an instruction which applies the Move method to the Charts collection:
The Move method allows you to specify which sheet the charts should be placed before or after.
To place the Charts collection to the left of all the existing sheets we can refer to item number 1 in the Sheets collection:
Pass a reference to the first sheet object to the Before parameter.
Run the subroutine and check the result in Excel:
The two charts will appear to the left of all the worksheets.
If we wanted to place the Charts collection to the right of the worksheets we could refer to item number 5 in the Sheets collection. The problem is that we might add more sheets later. To solve this, we can use the Count property of the Sheets collection. Alter the code to place the charts after the sheet whose index number is equal to the count of sheets:
Alter the code so that it looks like this.
Run the subroutine and check the results in Excel:
Now the charts will appear to the right of all the other sheets.
You can refer to objects embedded within another object using the number of the object. Create a new subroutine called ReferToEmbeddedObject and add an instruction to select the AFC 2017 worksheet:
Create a subroutine as shown here.
We'd like to refer to the second item in the ListObjects collection on the worksheet:
The worksheet contains four ListObject objects and we'd like to refer to the second one.
Add a variable to hold a reference to a ListObject and set a reference to the second item in the ListObjects collection on the worksheet:
Precede the ListObjects collection with a reference to the container object. In this case you can refer to the ActiveSheet.
Add an instruction to modify the TableStyle property of the object referenced by the variable:
Change the style of the table as shown here.
Run the subroutine and check the results in Excel:
The exact appearance of the table will depend on which theme the workbook uses.
To practise referring to objects by index number:
Sub MoveFirstSheetToLast()
ThisWorkbook.Activate
End Sub
Sub MoveFirstSheetToLast()
ThisWorkbook.Activate
Sheets(1)
End Sub
Sub MoveFirstSheetToLast()
ThisWorkbook.Activate
Sheets(1).Move After:=Sheets(Sheets.Count)
End Sub
Sub MoveLastSheetToFirst()
ThisWorkbook.Activate
End Sub
Sub MoveLastSheetToFirst()
ThisWorkbook.Activate
Sheets(Sheets.Count).Move Before:=Sheets(1)
End Sub
To practise referring to embedded objects:
Sub FormatSingleDataPoint()
Worksheets("NFL 2017").Select
End Sub
Sub FormatSingleDataPoint()
Dim co As ChartObject
Worksheets("NFL 2017").Select
Set co = ActiveSheet.ChartObjects(1)
End Sub
Sub FormatSingleDataPoint()
Dim co As ChartObject
Dim s As Series
Worksheets("NFL 2017").Select
Set co = ActiveSheet.ChartObjects(1)
Set s = co.Chart.SeriesCollection(2)
End Sub
Sub FormatSingleDataPoint()
Dim co As ChartObject
Dim s As Series
Dim p As Point
Worksheets("NFL 2017").Select
Set co = ActiveSheet.ChartObjects(1)
Set s = co.Chart.SeriesCollection(2)
Set p = s.Points(8)
End Sub
Sub FormatSingleDataPoint()
Dim co As ChartObject
Dim s As Series
Dim p As Point
Worksheets("NFL 2017").Select
Set co = ActiveSheet.ChartObjects(1)
Set s = co.Chart.SeriesCollection(2)
Set p = s.Points(8)
p.Format.Fill.ForeColor.RGB = rgbLime
End Sub
Running the code should highlight the data point for the team which lost every game in the season.
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.