View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Returning Part of a String

Sorry... cancel that... my function doesn't work if there isn't a
dot-extension.

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
If you order the tests properly, you can combine all this into a one-liner
(see my post from about an hour ago) that doesn't need to test the
positions the way your code does.

--
Rick (MVP - Excel)


"Harlan Grove" wrote in message
...
Ron Rosenfeld wrote...
...
Function fn(str As String) As String
Dim sTemp
sTemp = Split(str, "\")
fn = sTemp(UBound(sTemp))
fn = Left(fn, InStrRev(fn, ".") - 1)
End Function


This works, but using Split is somewhat wasteful. Also doesn't hurt to
add error checking in case the last token doesn't contain a period.

Function basename(s As String) As String
Dim p As Long, q As Long
p = InStrRev(s, "\") + 1
q = InStrRev(s, ".")
If q < p Then q = Len(s) + 1
basename = Mid(s, p, q - p)
End Function