Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Dan
 
Posts: n/a
Default Extract text from String

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
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Toppers
 
Posts: n/a
Default Extract text from String

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

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

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

  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Toppers
 
Posts: n/a
Default Extract text from String

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

  #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



  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,651
Default Extract text from String

On Fri, 30 Jun 2006 12:18:02 -0700, 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


Small change to return #VALUE! if delimiter is not present, as OP requested:

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

One other thought, and this really is up to the OP: This UDF will work on a
portion of the delimiter. In other words, it will return Peach if the
delimiter is specified as "4" or as "4h". And if the "fruits" are not all real
fruits, but could occasionally be strings that include a digit, things could
get confusing. (Or if the delimiter was a string that started with a letter).

Whether to take this into account or not would depend on the OP's real value
range for these strings.


--ron
  #7   Report Post  
Posted to microsoft.public.excel.worksheet.functions
CLR
 
Posts: n/a
Default Extract text from String

Maybe something along these lines........

=IF(ISERR(FIND(B1,A1,1)),"",IF(FIND(B1,A1,1)0,LOO KUP(B1,{"3h","7h"},{"Melon","Bananna"})))

Vaya con Dios,
Chuck, CABGx3



"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

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

This will only work if I know the IDs and the variables (information within
the LOOKUP). Unfortunately I will only be given an ID, 0h, 1h, 2h, ...

Regards,

Dan

"CLR" wrote:

Maybe something along these lines........

=IF(ISERR(FIND(B1,A1,1)),"",IF(FIND(B1,A1,1)0,LOO KUP(B1,{"3h","7h"},{"Melon","Bananna"})))

Vaya con Dios,
Chuck, CABGx3



"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

  #9   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Toppers
 
Posts: n/a
Default Extract text from String

My UDF (earlier posting) addresses this problem - ID only is required.

"Dan" wrote:

This will only work if I know the IDs and the variables (information within
the LOOKUP). Unfortunately I will only be given an ID, 0h, 1h, 2h, ...

Regards,

Dan

"CLR" wrote:

Maybe something along these lines........

=IF(ISERR(FIND(B1,A1,1)),"",IF(FIND(B1,A1,1)0,LOO KUP(B1,{"3h","7h"},{"Melon","Bananna"})))

Vaya con Dios,
Chuck, CABGx3



"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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reomving a set numer of characters from a text string ado1963 Excel Discussion (Misc queries) 3 January 10th 06 02:33 PM
Looking up a text string clubin Excel Discussion (Misc queries) 5 December 29th 05 04:14 PM
can you find specific text in a string ignoring any other text chriscp Excel Discussion (Misc queries) 1 September 18th 05 09:54 PM
EXTRACT TEXT FROM TEXT STRING carricka Excel Worksheet Functions 4 July 8th 05 11:00 AM
Extracting from a text string AmyTaylor Excel Worksheet Functions 3 June 24th 05 01:34 PM


All times are GMT +1. The time now is 11:52 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"