Roy has a formula that references a cell in another workbook, as ='[Timesheets.xlsm]Week01'!L6. He would like to have the formula pick up the name of the worksheet (Week01) from another cell, so that the formula becomes more general-purpose. Roy wonders how he should change the formula so it can use whatever worksheet name is in cell B9.
The easiest way to handle this is to use the INDIRECT worksheet function. Essentially, it takes a cell address and uses it as a "pointer" and then retrieves the value from that cell. For instance, you could use the following:
=INDIRECT(L6)
This results in the value in cell L6 being fetched and used as the pointer to what you really want. Thus, if cell L6 contains "A7", then the function returns the value of whatever is in cell A7. Interestingly, if you invoke the function in this way you get a different result:
=INDIRECT("L6")
The result is the contents of cell L6 instead of using the contents of cell L6 as a pointer.
With this information, you could easily put together an address that will work properly with the INDIRECT function:
=INDIRECT("'[Timesheets.xlsm]" & K2 & "'!L6")
This formula assumes that the name of the desired worksheet is stored in cell K2. If a valid worksheet name is not in that cell the formula returns a #REF error.
One important "gotcha" with this approach is that you've got to make sure that the workbook (Timesheets.xlsm) is open. INDIRECT won't reach into an unopen workbook. If it is not open, then you'll get a #REF error instead of the desired value.
If you want to make the formula even more general-purpose, you could pull the workbook name, worksheet name, and cell referene all from cells within your worksheet, as in this manner:
=INDIRECT("'[" & K1 & "]" & K2 & "'!" & K3)
In this instance, K1 would contain the workbook name (Timesheets.xlsm), K2 would contain the worksheet name ("Week01"), and K3 would contain the cell desired ("L6").
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (6069) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Excel allows you to easily convert values from decimal to other numbering systems, such as hexadecimal. This tip explains ...
Discover MoreUsers of the most recent versions of Excel have four different ways available to combine values into strings. Even those ...
Discover MoreNeed to know what day of the year a certain date is? You can figure it out easily using the formulas in this tip.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-04-08 03:35:47
Erik P
Thanks Alan. I use this alot but always forget syntax with speech marks. This was a very clear explanation.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments