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 ...
When you have multiple workbooks open you should take care not to make assumptions about which one will be affected when you run a subroutine. On this page you'll learn how to write code to move between workbooks, making sure that you're always in the right place.
When you have lots of workbooks open you need to know how to move between them.
You can click here to download the set of files needed for this page.
You can click here to download a file containing the finished code.
Start by downloading and extracting the set of files linked to in the Files Needed section above. Open the file called Q1 Total.xlsm as shown below:
You should find four files in the extracted folder. Open the one shown here.
The workbook you have opened contains a single worksheet with a table into which we want to copy some values:
The values we want to copy are stored in the other three workbooks in the folder.
Open the VBE and insert a new module into the project attached to the Q1 Total.xlsm workbook:
Insert a module into this project.
Now open the other three workbooks from the folder you extracted:
Open the three files shown highlighted here.
When you return to the VBE you'll find multiple projects listed in the Project Explorer:
Each open workbook is listed in the Project Explorer. Our code will be stored in the Q1 Total.xlsm workbook but needs to refer to the other workbooks.
Now that we have the files that we need open, we'll copy the total sales for January from the appropriate file. Start by creating a new subroutine in the module that you created earlier:
Feel free to create a different name for the subroutine.
You can refer to an open workbook using its name. Start by referring to the Workbooks collection:
Write the word workbooks followed by an open parenthesis.
Next, write the name of the workbook within a set of double quotes:
Write the name of the workbook as it is shown in the Project Explorer, including the filename extension.
Close the parentheses and then apply the Activate method to the workbook:
The Activate method will take you to the specified workbook when the code is executed.
When this code is executed, Excel will make the January workbook the active workbook:
The workbook will be activated on whichever worksheet was active the last time the workbook was active.
You may find that the January workbook already has the Total worksheet active but you can't guarantee that this will always be the case. Add code which selects the Total worksheet, followed by cell B7 and copy this cell:
Add three lines of code as shown here. These lines will affect whichever workbook is active when the code is executed.
We now need to return to the Q1 Total.xlsm workbook before pasting the copied value. Add a line of code to do this:
The name of the workbook to activate is Q1 Total.xlsm.
As there is only one worksheet in this workbook we don't need to select it. We can simply select the appropriate cell and paste the copied value into it:
Add two lines to select cell B3 and paste the value of the copied cell into it.
Execute the subroutine and check that you see the results as expected:
The result of running this subroutine.
You can refer to the workbook in which your code is stored using the ThisWorkbook property. Not only is this quicker than typing the name of the workbook, it also means that you don't need to edit your code if you save the workbook with a different name. Create a new subroutine which activates the Feb Sales.xlsx workbook and copies cell B7 on the Total worksheet:
The beginning of this procedure will look very similar to the previous one.
In order to return to the Q1 Total.xlsm workbook, refer to the ThisWorkbook property:
ThisWorkbook refers to the workbook in which the code you're writing is stored. In this case it's the Q1 Total.xlsm workbook.
Apply the Activate method to the workbook, then select cell B4 and paste the copied value into it:
The complete procedure should look like this.
Execute the subroutine and check the results in Excel:
At this point you should have the totals for January and February copied into the table.
Each subroutine that we've used so far uses the same instructions to select and copy the value from the Total worksheet. It makes sense to create a separate subroutine to hold this sequence of instructions and then call it from the other procedures. Create a new subroutine to select and copy the total sales:
You can copy and paste the three instructions from one of the other subroutines you have already created.
Now alter the Copy_Jan_Sales and Copy_Feb_Sales subroutines so that they each make a call to the new subroutine:
The two subroutines should now resemble this. The comments show you where to make changes to your code.
Try running each of the Copy_Jan_Sales and Copy_Feb_Sales subroutines to make sure that they still work.
Create one more subroutine called Create_Q1_Totals which calls the Copy_Jan_Sales and Copy_Feb_Sales subroutines:
This subroutine simply calls the other routines which copy the appropriate values.
Run the Create_Q1_Totals subroutine to check that it works.
To practise moving between workbooks, create a new subroutine which will copy the total sales for March into the summary workbook:
Sub Copy_Mar_Sales()
Workbooks("Mar Sales.xlsx").Activate
End Sub
Sub Copy_Mar_Sales()
Workbooks("Mar Sales.xlsx").Activate
Select_And_Copy_Total
End Sub
Sub Copy_Mar_Sales()
Workbooks("Mar Sales.xlsx").Activate
Select_And_Copy_Total
ThisWorkbook.Activate
End Sub
Sub Copy_Mar_Sales()
Workbooks("Mar Sales.xlsx").Activate
Select_And_Copy_Total
ThisWorkbook.Activate
Range("B5").Select
ActiveCell.PasteSpecial xlPasteValues
End Sub
After running the procedure you should see all of the monthly results in the table.
Sub Create_Q1_Totals()
Copy_Jan_Sales
Copy_Feb_Sales
Copy_Mar_Sales
End Sub
Sub Save_And_Close_Files()
ThisWorkbook.Save
Workbooks("Jan Sales.xlsx").Close
Workbooks("Feb Sales.xlsx").Close
Workbooks("Mar Sales.xlsx").Close
ThisWorkbook.Close
End Sub
From: | mriordan1 |
When: | 28 Jul 20 at 15:56 |
Hi
Thank you for the training! It is very helpful.
I am wondering what can be done if the name of one of the input files will change occasionally. As my team edits workbooks, they save new versions and change the name. Is there a way around this? Thank you
From: | Andy B |
When: | 28 Jul 20 at 17:03 |
You can use VBA to loop over all of the files in a folder, opening each, but it's not that easy. Probably your best bet for the moment is to include in your code a constant containing the name of the workbook you want to work with:
Public Const WorkbookName as string = "Something.xlsx"
You can then activate this, if open, by writing:
Workbooks(WorkbookName).Activate
Notice that here are no quotation marks, because you're using the workbook name. If the workbook name changes, you only have to change it then in one place.
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.