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: Extracting File Names from a Path.
by Allen Wyatt
(last updated January 8, 2018)
Barry has a worksheet in which a column contains many file paths. He would like a way to extract just the filename (the part to the right of the final backslash) from each path. He wonders if there is a quick way to do this without using Text to Columns feature.
There are several different ways, depending on whether you want to use a macro or not.
If your filenames are all the same length, then you can simply use the RIGHT function to pull out the last characters. (This formula assumes the full path and file name is in cell A1.)
This assumes that the filename is always 11 characters long, such as "text001.txt". If the filename is a different length in each instance, then this approach won't work. Instead, you can try this formula:
Note that the formula uses the SUBSTITUTE function twice. In each case it replaces the backslashes (\) with something else. The first time it replaces all of them with an ASCII value of 1 and the second it replaces them with nothing (an empty string) so that it can determine how many backslashes were in the original path. The MID function is used to locate (with the help of FIND and the SUBSTITUTE functions) the location of the last backslash in the path and return everything after that point.
A shorter formula can be used if you are sure that the filename will never be more than 99 characters long:
This formula replaces all the backslashes with 100 spaces, grabs the right-most 99 characters from the resulting string (that would be the filename with a bunch of spaces in front of it) and then trims off all the spaces.
If you want to use a macro you can create a very short function that will pull apart a string (the full path, in this case) based upon delimiters:
Function GetFileName2(File_Path) As String Dim Parts Parts = Split(File_Path, Application.PathSeparator) GetFileName2 = Parts(UBound(Parts)) End Function
Told you it was short! The function that does the heavy work is the Split function which pulls a string apart based upon a delimiter you specify and stuffs the parts into an array. In this example the Split function uses as a delimiter whatever path separator is appropriate for the system on which Excel is running.
The last element of the resulting array (determined with the UBound function) contains the portion of the original path that is to the right of the last path separator—the file name. To use the function, put a formula like this in a cell:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12903) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Extracting File Names from a Path.
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!
Given a range of cells containing values, you may have a need to find the first value in the range that is unique. This ...Discover More
Need to count the number of W (win) or L (loss) characters in a range of cells? You can develop a number of formulaic ...Discover More
In a series of values you may need to know the smallest value that isn't a zero. There is no built-in function to do ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.