"Ron Rosenfeld" wrote in message
...
On Fri, 15 Feb 2008 23:00:40 -0500, "Rick Rothstein \(MVP - VB\)"
wrote:
The above as a "one-liner" in deference to Rick:
LOL
============================================
Function fn(str As String) As String
fn = Trim(Split(Split(str, "\")(UBound(Split _
(str, "\"))), "-")(LBound(Split(Split _
(str, "\")(UBound(Split(str, "\"))), "-"))))
End Function
==========================================
The LBound for a Split is always 0 no matter what the Option Base is set
to.
Using this fact, your one-liner can be simplified considerably...
Function fn(str As String) As String
fn = Trim(Split(Split(str, "\")(UBound(Split(str, "\"))), "-")(0))
End Function
Rick
Actually, neither your one liner nor my longer variants will work if
filename
includes a "-". For that, we need something like:
==============================
Option Explicit
Function fn(str As String) As String
Dim s1() As String
s1 = Split(str, "\")
s1 = Split(s1(UBound(s1)), "-")
ReDim Preserve s1(UBound(s1) - 1)
fn = Trim(Join(s1, "-"))
End Function
===========================
I thought I had corrected it as per your observation after the OP posted his
sample text line ... use " - ", not "-", in the one-liner...
Function fn(str As String) As String
fn = Trim(Split(Split(str, "\")(UBound(Split(str, "\"))), " - ")(0))
End Function
Of course, this supposes the filename itself does not contain dash
surrounded by spaces.
Rick