Parsing out a filename from a string

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
521
I have a list of UNC paths where I want to parse out only the name of the file and I'm having some difficulty. For example, using any of the scenarios below I want to extract "FILENAME.XLS" from the string. The file name and extension will vary in addition to the name and lengths of the folder paths.

varName = "\\server_name\share_name\folder1_name\FILENAME.XLS"
varName = "\\server_name\share_name\folder1_name\folder2_name\FILENAME.XLS"
varName = "\\server_name\share_name\folder1_name\folder2_name\folder3_name\FILENAME.XLS"

I used InStrRev to determine the position of the last \, Len to get the length of the string and subtracted the second from the first to get the file length.
Code:
var1 = Len(varName)
var2 = InStrRev(varName, "\")
var3 = (var1 - var2)
I assume that's what I needed to do, but how do I go about the next step and return the file name as a string to var4?
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
521
Never mind, I figure out I could use Right to give me what I needed.
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,955
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
You could use MID and eliminate a few steps:

var4 = Mid(varName, InStrRev(varName, "\") + 1)
 

Watch MrExcel Video

Forum statistics

Threads
1,109,367
Messages
5,528,271
Members
409,813
Latest member
robyrux

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top