Get file name?

nikko50

Board Regular
Joined
Mar 3, 2004
Messages
155
Hi all,

How to I get the file name of the variable FilePath. Basically just want to get filename in this case it would be "my doc 5.xls"

FilePath = "X:\users\slef\2015\1 June\my doc 5.xls"
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi,

try this:

Code:
Sub ShowFileName()
Dim FilePath As String, FileName As String


FilePath = "X:\users\slef\2015\1 June\my doc 5.xls"
FileName = Mid$(FilePath, InStrRev(FilePath, "\") + 1)


MsgBox FileName


End Sub

Dave
 
Upvote 0
Rather than use their formula, I think I would use this one instead...

=TRIM(RIGHT(SUBSTITUTE(A1,"\",REPT(" ",300)),300))

Also, instead of their multi-line subroutine, I think I would just use the coded equivalent of the manual method they posted...
Code:
Sub ExtractfilePath()
  ActiveSheet.Range("A1:A10").Replace "*\", "", xlPart
End Sub
 
Upvote 0
Also, instead of their multi-line subroutine, I think I would just use the coded equivalent of the manual method they posted...
Code:
Sub ExtractfilePath()
  ActiveSheet.Range("A1:A10").Replace "*\", "", xlPart
End Sub
Actually, since I am pretty sure Replace only works on the UsedRange, I would write the macro this way instead (ActiveSheet is the default, so I have eliminated it also)...
Code:
Sub ExtractfilePath()
  Columns("A").Replace "*\", "", xlPart
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,530
Messages
6,120,071
Members
448,943
Latest member
sharmarick

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top