View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default Extract a text string based on character

On Thu, 13 Mar 2008 07:39:04 -0700, kgiraffa
wrote:

I am trying to extract a model number from the title.

Brand New D-Link AirPlus G DI-524 Wireless Router;

I need to extract the DI-524. All of the model numbers have the - charcter,
but are different lengths. How would I use the mid function, not knowing the
length and exact position?

For instance, here is another title:

**New** Acer TravelMate TM4720-6218, 2.2GHz Core 2 Duo;

I need the TM4720-6218 extracted from the text string.

Thank you for your help!


With the information you have given, it is not possible without manual editing.
Your description of a model number -- having the "-" character -- does not
return only model numbers. For example, in your first example, a routine that
would return words with "-"'s would also return D-Link.

If we could be guaranteed that the model designation will always be the LAST
word in the string that contains a "-", and also that there will not be an
<space within the model designation, then this UDF can be used to return it:

================================
Option Explicit
Function Model(str As String) As String
Dim re As Object, mc As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "\b\S+-\S+\b"
If re.test(str) = True Then
Set mc = re.Execute(str)
Model = mc(mc.Count - 1)
End If
End Function
================================

To enter the UDF, <alt-F11 opens the VBEditor. Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code above into the window that opens.

To use this, enter a formula =Model(cell_ref) into some cell where cell_ref
refers to the cell containing the string with the model number.

This algorithm could be done with worksheet formula, but it is involved and I'd
rather wait to see exactly what your specifications are, before going further.
--ron