Return file from a path string

S

Steph

I have a string variable that is a path to a file:
C:\folder1\folder2\folder3\folder4\folder5\file.xls

How can I create a new variable that is simply the file name.xls? Thanks!
 
T

Tom Ogilvy

If you have xl2000 or later:

sStr = "C:\folder1\folder2\folder3\folder4\folder5\file.xls"
? Right(sStr,len(sStr)-instrrev(sStr,"\"))
file.xls

demo's from the immediate window.
 
B

Bob Phillips

One way Steph

Dim sFile
Dim ary

sFile = "C:\folder1\folder2\folder3\folder4\folder5\file.xls"

ary = Split(sFile, "\")
MsgBox ary(UBound(ary))

or more directly, but less efficiently

Dim sFile

sFile = "C:\folder1\folder2\folder3\folder4\folder5\file.xls"
MsgBox Split(sFile, "\")(UBound(Split(sFile, "\")))


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
S

Steph

Thank you so much

Tom Ogilvy said:
If you have xl2000 or later:

sStr = "C:\folder1\folder2\folder3\folder4\folder5\file.xls"
? Right(sStr,len(sStr)-instrrev(sStr,"\"))
file.xls

demo's from the immediate window.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top