Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Getting the Name of the Parent Workbook.

Getting the Name of the Parent Workbook

by Allen Wyatt
(last updated January 31, 2015)

9

Many people use the CELL worksheet function to return the name of the current Excel workbook. A common way to use the function is as follows:

=CELL("filename")

Using the CELL function in this manner is fine, provided you only have one workbook open at a time. If you open more than one, then this usage can cause problems. Why? Because when used this way, CELL returns the name of the currently active workbook, not the workbook in which the formula is used.

To always return the name of the workbook in which CELL is used (sometimes called the "parent workbook"), you must alter the formula just a bit:

=CELL("filename", A1)

By adding a cell reference as the second parameter in the function, you are telling Excel that you want the name of the file containing that cell reference. In other words, CELL will return the name of the file in which cell A1 of the current worksheet is located. (You can also provide any other cell reference in place of A1, if more appropriate.)

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10771) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Getting the Name of the Parent Workbook.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Ignoring Case in a Comparison

Do you want Excel to take the case of your text into account when it does comparisons in a formula? The IF statement ignores ...

Discover More

Missing Left Border

Ever wonder why a border around a graphic doesn't print the way it looks on the screen? There are several ways to add and ...

Discover More

Putting a Different Date in a Header

Today's date is easy to add to a header, but what if you want to add a date that is adjusted in some manner? Adding ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Ways to Concatenate Values

Users of the most recent versions of Excel have four different ways available to combine values into strings. Even those ...

Discover More

Returning an ANSI Value

Need to know the character value of the first character in a string? It's easy to do, without using a macro, by using the ...

Discover More

Strange ATAN Results

You may use Excel's trigonometric functions to do some quick calculations, and suddenly notice that the results in your ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four less than 9?

2016-04-08 14:20:07

Michael (Micky) Avidan

@Dennis Costello,
For all those who make "odd choices" - please check the following formula which seems to be, significant enough, shorter than yours .
I didn't compare their length because your formula was, surprisingly, spread on too many rows.
--------------------------------------
=REPLACE(LEFT(SUBSTITUTE(CELL("filename"),MID(CELL("filename"),1,FIND("[",CELL("filename"))-1),),FIND("||",SUBSTITUTE(SUBSTITUTE(CELL("filename"),MID(CELL("filename"),1,FIND("[",CELL("filename"))-1),),".","||",LEN(SUBSTITUTE(CELL("filename"),MID(CELL("filename"),1,FIND("[",CELL("filename"))-1),))-LEN(SUBSTITUTE(SUBSTITUTE(CELL("filename"),MID(CELL("filename"),1,FIND("[",CELL("filename"))-1),),".",))))-1),1,1,)
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL


2016-04-07 08:50:18

Dennis Costello

Michael Avidan ... your formula is the more practical one to use in most cases - in fact, I've used one almost identical to it for years for that task. After all, for spreadsheets the filetype is likely to be .XLS (often plus one more letter).

Of course, if someone makes the odd choice of naming their spreadsheet foobar.xls1234.xlsm, your simpler formula will give the wrong answer and my longer one will be right - which is the case PRodrigues dealt with, too.


2016-04-06 10:46:04

Michael (Micky) Avidan

#Dennis Costelloת
Also your long formula in conjunction of using helper(!) cells has bothered me for a while, so I took some time, today, to shorten it into a single cell.
Try:
=MID(CELL("filename",A1),FIND("[",CELL("filename",A1))+1,FIND(".xls",LOWER(CELL("filename",A1)))-FIND("[",CELL("filename",A1))-1)
The above formula handles as many periods that are included in the file's name.
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL


2016-04-05 11:41:07

Dennis Costello

Hi ... The reeaaalllyyy long formula has bothered me for a while, so I took some today to deconstruct it. Observations:

- you don't need to use INDIRECT("A1") to avoid #REF errors when column A or row 1 is deleted - just use the current cell address. Deleting the formula's row or column clearly deletes the formula itself.

- the actual filename is the portion of the CELL function's string that lies between [ and ]

- the stated goal is to return everything but the last period in that actual filename. This can be done a lot more simply than in your formula:

put
=CELL("filename",A1) into cell A1 (the complete filename, which also includes the worksheet name)
= MID(A1, FIND("[", A1) + 1, FIND("]", A1) - FIND("[", A1) - 1) into A2 (the "actual" filename)
, then

=LEFT(
A2,
FIND("*",
SUBSTITUTE(A2, ".", "*", LEN(A2) - LEN(SUBSTITUTE(A2, ".", "")))) - 1)

gives you the filename part by itself

In other words, figure out how many periods are in the actual filename (the difference in string length between the filename and a copy of it with all the periods removed), use that as a counter for which of the periods to replace with an illegal character (the "*"), and then return everything up to that sentinel character using the LEFT function.

As a single formula (not relying on the results of other cells), this takes this form:
=LEFT(
MID(
CELL("filename", A1),
FIND("[", CELL("filename", A1)) + 1,
FIND("]", CELL("filename", A1)) - FIND("[", CELL("filename", A1)) - 1),
FIND(
"*",
SUBSTITUTE(
MID(
CELL("filename", A1),
FIND("[", CELL("filename", A1)) + 1,
FIND("]", CELL("filename", A1)) - FIND("[", CELL("filename", A1)) - 1),
".",
"*",
LEN(
MID(
CELL("filename", A1),
FIND("[", CELL("filename", A1)) + 1,
FIND("]", CELL("filename", A1)) - FIND("[", CELL("filename", A1)) - 1)) -
LEN(
SUBSTITUTE(
MID(
CELL("filename", A1),
FIND("[", CELL("filename", A1)) + 1,
FIND("]", CELL("filename", A1)) - FIND("[", CELL("filename", A1)) - 1),
".",
"")
)
)
) - 1)


2015-02-03 05:11:01

PRodrigues

Hello b rad

You formula has a problem!
Imagine that the file name is Test.1.xls
With your formula you will only get
"Test".
You need a expression to get all til the last ocurrence of "." minus 1.

Just for fun (is long: 3764 chars), in only one cell (bullet prof: you can do "bad" things to the A1 cell (like delete column A, ou line 1, or move something to A1), i put my solution (enjoy)

=LEFT(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1),LEN(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))-LEN(IF((LEN(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))))-LEN(SUBSTITUTE(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))),".","")))=0,"",RIGHT(IF((LEN(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))))-LEN(SUBSTITUTE(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))),".","")))=0,"",SUBSTITUTE(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))),".","*",(LEN(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))))-LEN(SUBSTITUTE(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))),".",""))))),LEN(IF((LEN(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))))-LEN(SUBSTITUTE(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))),".","")))=0,"",SUBSTITUTE(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))),".","*",(LEN(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))))-LEN(SUBSTITUTE(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))),".",""))))))-FIND("*",IF((LEN(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))))-LEN(SUBSTITUTE(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))),".","")))=0,"",SUBSTITUTE(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))),".","*",(LEN(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))))-LEN(SUBSTITUTE(PROPER(TRIM(MID(CELL("filename",INDIRECT("A1")),FIND("[",CELL("filename",INDIRECT("A1")))+1,FIND("]",CELL("filename",INDIRECT("A1")))-FIND("[",CELL("filename",INDIRECT("A1")))-1))),".","")))))))))-1)

Best regards

PRodrigues


2015-02-02 11:44:17

b rad

For just the file name without the .xls* extension, try this.

=MID(CELL("filename", A1),SEARCH("[",CELL("filename", A1))+1, SEARCH(".",CELL("filename", A1))-SEARCH("[",CELL("filename", A1))-1)


2015-02-01 11:02:46

sarma

Yes,it works!! Thanks


2015-01-31 08:10:21

PRodrigues

Yes. Just paste

=MID(CELL("filename";INDIRECT("A1"));FIND("[";CELL("filename";INDIRECT("A1")))+1;FIND("]";CELL("filename";INDIRECT("A1")))-FIND("[";CELL("filename";INDIRECT("A1")))-1)

to any cell. The file must have a name.

NOTE: You can have a simplier version, but not bullet proof.
In the first version, you can do "bad" things to the A1 cell (like delete column A, ou line 1, or move something to A1).

The simplier version follows:
In any cell (except A1) paste:
=MID(A1;FIND("[";A1)+1;FIND("]";A1)-FIND("[";A1)-1)

In cell A1, paste
=CELL("filename";A1)
Note that you will not have the CIRC indicator


2015-01-31 05:57:55

sarma

Great tip. Is it possible to return only the filename without the entire path?


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.