Get filename only

  • Thread starter Thread starter kaon
  • Start date Start date
K

kaon

Dear all,

How can I just return the filename only (i.e without path) as an
absolute path can be returned by:
fName = Application.GetOpenFilename(FileFilter:="All files (*.*),
*.*", _
Title:="Select a file",
MultiSelect:=False)

Thanks.
 
Hi
try the following (will not work in Excel 97 due to InStrRev)
sub foo()
dim fname
fname = application.getopenfilename (FileFilter:="All files (*.*),
*.*", _
Title:="Select a file", MultiSelect:=False)
fname = mid(fname,instrrev(fname,"\")+1,255)
msgbox "You have entered the name:" & fname
end sub
 
Hi kaon

For Excel 97 - 2003 you can use this

Sub foo2()
Dim vArr As Variant, fname As Variant
Dim sFileNameXls As String
fname = Application.GetOpenFilename(FileFilter:="All files (*.*),*.*", _
Title:="Select a file", MultiSelect:=False)
vArr = Split97(fname, "\")
sFileNameXls = vArr(UBound(vArr))
MsgBox "You have entered the name:" & sFileNameXls
End Sub

Function Split97(sStr As Variant, sdelim As String) As Variant
'Tom Ogilvy
Split97 = Evaluate("{""" & _
Application.Substitute(sStr, sdelim, """,""") & """}")
End Function


--
Regards Ron de Bruin
http://www.rondebruin.nl


Frank Kabel said:
Hi
try the following (will not work in Excel 97 due to InStrRev)
sub foo()
dim fname
fname = application.getopenfilename (FileFilter:="All files (*.*),
*.*", _
Title:="Select a file", MultiSelect:=False)
fname = mid(fname,instrrev(fname,"\")+1,255)
msgbox "You have entered the name:" & fname
end sub
 
Hi

Why not:

fname = dir(fname)

Regards
Hamilton R. Romano

Ron de Bruin said:
Hi kaon

For Excel 97 - 2003 you can use this

Sub foo2()
Dim vArr As Variant, fname As Variant
Dim sFileNameXls As String
fname = Application.GetOpenFilename(FileFilter:="All files (*.*),*.*", _
Title:="Select a file", MultiSelect:=False)
vArr = Split97(fname, "\")
sFileNameXls = vArr(UBound(vArr))
MsgBox "You have entered the name:" & sFileNameXls
End Sub

Function Split97(sStr As Variant, sdelim As String) As Variant
'Tom Ogilvy
Split97 = Evaluate("{""" & _
Application.Substitute(sStr, sdelim, """,""") & """}")
End Function
 
Hi Hamilton

When I want to know this a few years back Tom give me this example and
I always use it since then.

But Dir(fname) Is OK for me<g>

Have a nice day
 
I suggested the dir(fname), too for a poster who was retrieving lots and lots of
files.

Tom suggested instrrev (or some variation). Lots of disk access would slow down
the procedure more than the instrrev (or some variation).

Made sense to me (well, after I was corrected!).
 

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

Back
Top