Parse a string in VBA?

  • Thread starter Thread starter Terry
  • Start date Start date
T

Terry

How would I go about extracting a portion of a string that comes before a selected chasracter. In
my case I can find the last occurrance of a "-" (dash) using the inStrRev() function, but what do I
do with the returned value to break up the string?

Terry
 
Check Help for Left, Mid, and Right.

HTH
Ed

Terry said:
How would I go about extracting a portion of a string that comes before a selected chasracter. In
my case I can find the last occurrance of a "-" (dash) using the
inStrRev() function, but what do I
 
have a look at mid(dddd,yourfind,22)
the left, right, & mid functions will help, depending on what you want to
do.

--
Don Guillett
SalesAid Software
(e-mail address removed)
Terry said:
How would I go about extracting a portion of a string that comes before a selected chasracter. In
my case I can find the last occurrance of a "-" (dash) using the
inStrRev() function, but what do I
 
Terry

not sure if you are trying to do this in a worksheet or in VBA Code.
Anyway, let's assume you define a UDF, say:

Function UseInStrRev(Target As Range) As Long
UseInStrRev = InStrRev(Target, "-")
End Function

Then, in the worksheet, you could use: =UseInStrRev(E19) to get the
position

text-text-abcde: would give a value of 10

And then you could use: =LEFT(E19,UseInStrRev(E19)-1) to get the
string to the left of the last "-"

which would result in: text-text

Regards

Trevor


Terry said:
How would I go about extracting a portion of a string that comes before a selected chasracter. In
my case I can find the last occurrance of a "-" (dash) using the
inStrRev() function, but what do I
 
Would any ideas here help?

Sub Demo()
Dim s As String
Dim v As Variant
s = "This-is-a-text-string"
v = Split(s, "-")
MsgBox "Last item is the word: " & v(UBound(v))
End Sub

--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


Terry said:
How would I go about extracting a portion of a string that comes before a selected chasracter. In
my case I can find the last occurrance of a "-" (dash) using the
inStrRev() function, but what do I
 
Dana said:
Would any ideas here help?

Sub Demo()
Dim s As String
Dim v As Variant
s = "This-is-a-text-string"
v = Split(s, "-")
MsgBox "Last item is the word: " & v(UBound(v))
End Sub
Thanks for all those good answers...and my gosh, I didn't know that VBA had a Split function???

Terry
 
Back
Top