View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Dan
 
Posts: n/a
Default Extract text from String

Thanks for the assistance. Works great.

"Toppers" wrote:

Copy and paste the macro code (Function Getstring ....) below into a module
and then in a cell , e.g B1, put:


=Getstring(A1,"7h")

Where A1 contains your string and "7h" is the ID

B1 will contain (or should!) the text associated with the ID.

This works in the same as Excel functions such as SUM etc.

Copy/paste this code:

Function GetString(ByVal strSearch As String, ByVal delimeter As String)
n1 = InStr(1, strSearch, delimeter)
If n1 = 0 Then
GetString = "no match"
Exit Function
End If
n2 = InStrRev(Left(strSearch, n1 - 2), " ") + 1
GetString = Mid(strSearch, n2, n1 - n2 - 1)
End Function

HTH

"Dan" wrote:

My apologies, but I am a little unfamilar with creating the function call.
Would I create a macro called "GetString" then

Sub GetString()
... details below?
End Sub

Regards,
Dan

"Toppers" wrote:

UDF:

=Getstring(A1,"7h")

assumes a blank between each text pair.

A1 contains string,
second parameter is ID code

Function GetString(ByVal strSearch As String, ByVal delimeter As String)
n1 = InStr(1, strSearch, delimeter)
If n1 = 0 Then
GetString = "no match"
Exit Function
End If
n2 = InStrRev(Left(strSearch, n1 - 2), " ") + 1
GetString = Mid(strSearch, n2, n1 - n2 - 1)
End Function

HTH

"Dan" wrote:

Trying to extract text within a string (do not know the length or content).
Only information I will be given is the ID corresponding to the text needed
to extract.

String:
Apple 0h Orange 1h Pear 2h Melon 3h Peach 4h Bananna 7h

For the ID "3h", I need to return Melon.
For the ID "7h", I need to return Bananna.

There is the possibility of an ID not in the string, i.e. 5h which should
return #VALUE!. The problem is 7h.

Thanks

Dan