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 ...
If you want to select cells in different worksheets, it's important to select the correct worksheet first. This page shows you several techniques for moving between worksheets in a workbook.
To select a cell on a worksheet, you must first make sure that the sheet is selected.
You can click here to download the file needed for this page.
You can click here to download a file containing the completed code.
You can switch between worksheets using one of several different techniques. To begin demonstrating this, extract and open the file linked to in the Files Needed section above:
The sample workbook contains four worksheets, each of which contains details of drinks sold in a branch of a chain of coffee shops.
When you refer to a cell in your code, Excel assumes the cell is on the worksheet which is currently active when the code is executed. As shown in the screenshot below, you should find that the Paris worksheet is active when you open the sample workbook:
You can see the tab of the active sheet highlighted in a different colour.
We'd like to change the title in cell B2 to match the name of the worksheet. In the VBE, insert a new module and create a new subroutine as shown below:
Create a new subroutine as shown.
Add some code to select cell B2 and then change the value of the selected cell to match the name of the selected worksheet:
You can use the ActiveSheet property to return a reference to the worksheet that is currently active when the code is executed.
Run the subroutine and check the results:
You should find that the value of B2 has changed only on the sheet that was selected when you executed the code.
One way to move to another worksheet is to reference it by name and apply the Select method to it. Create a new subroutine which will select the London worksheet:
Begin the first instruction by referring to the Worksheets collection followed by an open parenthesis.
To specify which worksheet you want to reference, you can enter its name within a set of double-quotes:
Enter the name of the worksheet within a set of double-quotes. The name of the sheet isn't case-sensitive but it's good practice to match the name as it appears on the worksheet tab in Excel.
You can then apply methods and properties to the worksheet object. In this example we'll apply the Select method to make London the active sheet:
Apply the Select method to the worksheet object.
Once the London sheet has been selected, you can refer to and modify properties of cells in the usual way:
We've used the same code to select and alter the value of a cell as in the first example.
Execute the procedure and check the result in Excel:
You should see the London sheet has been selected and the value of cell B2 has been changed.
You can also refer to a worksheet using a number which indicates its position in the set of worksheet tabs in the workbook. In the next example, we want to change something on the Milan worksheet:
Counting from left to right, Milan is the third worksheet.
Begin a new subroutine and refer to the Worksheets collection:
Begin the instruction in the same way as in the previous example.
Rather than entering the name of a worksheet, you can simply enter the number which corresponds to its position:
Milan is the third worksheet so enter the number 3 and close the parentheses. You can then apply the Select method to the worksheet.
The remaining code is the same as we used in the previous two examples:
Feel free to copy and paste this code instead of writing it again!
When you run the subroutine you should find that the third worksheet becomes selected and the value of cell B2 will change:
This is what you should see after running the subroutine.
For the final and most exotic of the four locations, we'll select the worksheet using a codename. All worksheets begin life with a default codename which you can see in the Project Explorer window:
The code name of the Hull worksheet is currently Sheet4.
You can use the original codename of a worksheet but it will make your code easier to understand if you give a more descriptive name. You can change the codename of a worksheet using the Properties window:
Select the worksheet in the Project Explorer and then edit its (Name) property in the Properties window. Here we're calling the worksheet HullSheet.
Press Enter once you've finished typing in a new codename for the worksheet:
The new codename of the worksheet will be displayed in the Project Explorer.
Once you've changed the codename you can use it in a subroutine as shown below:
You can write the codename of the worksheet and apply methods and properties to it directly. You'll even see the IntelliSense list appear when you refer to a worksheet using this technique!
The remaining code to select and modify a cell is the same as we've used in the previous examples on this page:
Select and then change the value of cell B2.
When you run the subroutine you should the results shown below:
The Hull worksheet will be selected and cell B2's value will match the name displayed on the sheet tab.
Using a sheet codename is an easy and convenient way to refer to a worksheet but beware that this technique only works when the worksheet that you want to reference is in the same workbook as your code.
Click here to download a file containing the sample data. Extract and open this file to see an extended version of the file you've been working on in this section:
This version of the workbook contains an extra worksheet in which we'd like to display the total sales from each branch of the shop.
Your task is to copy the total sales from each of the other four worksheets into the Total sheet.
To practise selecting worksheets by name:
Sub Copy_Paris_Total()
Worksheets("Paris").Select
End Sub
Sub Copy_Paris_Total()
Worksheets("Paris").Select
Range("F9").Select
ActiveCell.Copy
End Sub
Sub Copy_Paris_Total()
Worksheets("Paris").Select
Range("F9").Select
ActiveCell.Copy
Worksheets("Total").Select
End Sub
Sub Copy_Paris_Total()
Worksheets("Paris").Select
Range("F9").Select
ActiveCell.Copy
Worksheets("Total").Select
Range("B3").Select
ActiveCell.PasteSpecial xlPasteValues
End Sub
You should find that the Total worksheet contains the total sales from the Paris sheet.
To practise selecting worksheets by position:
Sub Copy_London_Total()
Worksheets(2).Select
End Sub
Sub Copy_London_Total()
Worksheets(2).Select
Range("F9").Select
ActiveCell.Copy
End Sub
Sub Copy_London_Total()
Worksheets(2).Select
Range("F9").Select
ActiveCell.Copy
Worksheets(5).Select
End Sub
Sub Copy_London_Total()
Worksheets(2).Select
Range("F9").Select
ActiveCell.Copy
Worksheets(5).Select
Range("B4").Select
ActiveCell.PasteSpecial xlPasteValues
End Sub
You should find the total sales from the London sheet in the Total sheet.
Click the worksheet to select it.
Change the (Name) property and press Enter.
Change the codename of this worksheet to TotalSheet.
Sub Copy_Milan_Total()
MilanSheet.Select
Range("F9").Select
ActiveCell.Copy
End Sub
Sub Copy_Milan_Total()
MilanSheet.Select
Range("F9").Select
ActiveCell.Copy
TotalSheet.Select
Range("B5").Select
ActiveCell.PasteSpecial xlPasteValues
End Sub
This is what you should see after running the subroutine.
To finish this example, create a subroutine to copy the total from the Hull worksheet into the Total sheet using whichever technique you prefer:
The end result should resemble this.
Save and close the workbook. You can click here to download the finished version of this workbook.
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.