Method 1 – Use Find and Replace Feature to Get Filename from Path
Steps:
- Select cells (B5:B11) from the worksheet with paths.
- Selecting cells press Ctrl+H from the keyboard.
- A new window will appear. From the appeared window, type “*\” in the “Find what” box and click “Replace All”.
- Get the filename from the path within the same column.
Method 2 – Combine LEN, SUBSTITUTE, FIND, and MID Functions to Get Filename from PathSteps:
- Select a cell (C5) and write the following formula down-
=MID(B5,FIND("*",SUBSTITUTE(B5,"\","*",LEN(B5)-LEN(SUBSTITUTE(B5,"\",""))))+1,LEN(B5))
- LEN(B5): Here, the LEN function returns the total length which is 34.
- LEN(SUBSTITUTE(B5,”\”,””): The SUBSTITUTE function substitutes this “\” character with blanks resulting in “C:UsersPublicPicturesDeer baby”. The LEN function returns the total length which is 30.
- SUBSTITUTE(B5,”\”,”*”,34-30): In this argument, the SUBSTITUTE function returns the character substituting with “*”.
- FIND(“*”,C:\Users\Public\Pictures*Deer baby): The FIND function will find the position of the character(*) displaying an output of 25 meaning it’s in the 25th position.
- =MID(B5,25+1, LEN(B5): The MID function extracts 30 characters from the string, starting from the 25th position. The result stands as “Deer baby”.
Method 3 – Combine REPT, SUBSTITUTE, RIGHT, and TRIM Functions to Get Filename from Path
Steps:
- Choose a cell (C5) and write the following formula down-
=TRIM(RIGHT(SUBSTITUTE(B5,"\",REPT(" ",100)),99))
- Hit Enter and pull the “fill handle” to get the result in all cells.
Method 4 – Combine CHOOSECOLS and TEXTSPLIT Functions to Get Filename from Path
Steps:
- Choose a cell (C5) and write the following formula down-
=CHOOSECOLS(TEXTSPLIT(B5,"\"),-1)
- Hit Enter and pull the “fill handle” to get the result in all cells.
Method 5 – Merge SUBSTITUTE, LEN, CHAR, FIND, MID, and IFERROR Functions to Get Filename Without Extension from Path
Steps:
- Choose a cell (C5) to apply the formula-
=IFERROR(MID(B5,FIND(CHAR(1),SUBSTITUTE(B5,"\",CHAR(1),LEN(B5)-LEN(SUBSTITUTE(B5,"\",""))))+1,FIND(CHAR(1),SUBSTITUTE(B5,".",CHAR(1),LEN(B5)-LEN(SUBSTITUTE(B5,".",""))))-FIND(CHAR(1),SUBSTITUTE(B5,"\",CHAR(1),LEN(B5)-LEN(SUBSTITUTE(B5,"\",""))))-1),"")
- Enter the key and then drag the “fill handle” down.
- Within a moment, your output will be in your hands.
Method 6 – Create a User Defined Function to Get Filename from Path
Steps:
- Press Alt+F11 to open the “Microsoft Visual Basic for Applications”.
- Click the “Module” option from the “Insert” feature.
- In the module put the following code-
Function GetFileName(FullPath As String) As String
Dim List As Variant
List = VBA.Split(FullPath, "\")
GetFileName = List(UBound(List, 1))
End Function
- Without moving here and there, just save the code and minimize the VBA window.
- Choose a cell (C5), type your defined function within the cell, and put the cell (B5) inside the brackets of the defined function-
=GetFileName(B5)
- Press Enter and drag down the “fill handle” to get the output inside the new column.
Method 7 – Apply VBA Code to Get Filename from Path
Steps:
- Press Alt+F11 to open the “Microsoft Visual Basic for Applications” window.
- Select “Module” from the “Insert” option.
- Inside the new module write the following code down and press the “Run” icon from the top ribbon-
Sub filePath()
Dim filename As String
Dim x As Variant
For Each cell In ActiveSheet.Range("B5:B11")
x = Split(cell.Value, Application.PathSeparator)
filename = x(UBound(x))
cell.Value = filename
Next cell
End Sub
- Get only the filename from the path in your Excel worksheet.
Things to Remember
In method 7, I have chosen a range (B5:B11) as I am applying the code for those cells. If you are applying the same VBA code don’t forget to change the cell range.
Download Practice Workbook
Download this practice workbook to exercise while you are reading this article.
<< Go Back to Excel Files | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!
or you could just use =CHOOSECOLS(TEXTSPLIT(a1,””),-1)
Hi Liam,
Thank you for taking the time to comment on our blog and for suggesting a shorter method for the task that is demonstrated. We appreciate your input and always welcome new ideas.
After carefully considering your formula, I noticed there was a backslash “\” missing from the col_delimiter argument of the TEXTSPLIT() function inside the formula. To ensure the best results, modify your formula like this:
=CHOOSECOLS(TEXTSPLIT(A1,"\"),-1)
(Assuming the full path address is inside the cell A1).We value your engagement and encourage you to keep sharing your thoughts and ideas. It’s through constructive discussions like this that we can all learn and improve. Thanks again for your comment!
Regards,
ExcelDemy.