**Please Note: **
This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. 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.

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, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: **Getting the Name of the Parent Workbook**.

**Program Successfully in Excel!** John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out *Excel 2013 Power Programming with VBA* today!

Need to know what day of the year a certain date is? You can figure it out easily using the formulas in this tip.

Discover MoreVLOOKUP is a great function to use in accessing data based on a lookup value. Problem is, you can't easily return ...

Discover MoreThe PROPER worksheet function is used to change the case of text so that only the first letter of each word is uppercase. ...

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

2016-04-08 14:20:07

Michael (Micky) Avidan

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

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

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

- 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

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

=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

=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?

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 © 2020 Sharon Parq Associates, Inc.

## Comments