Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Worksheet Functions / February 2008

Tip: Looking for answers? Try searching our database.

Extracting string within a string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ashg657 - 07 Feb 2008 11:21 GMT
Complex one here I think.....

I have a coumn of strings, for example:

CPSAINSBURYSAE0401001
CPAE0401001SAINSBURYS

..what I need to be able to do is extract from the strings the following:

LLNNNNNNN
(L= Letter, N = Number)
So if we applied this to the above examples, we would have a result of:

AE0401001

Little bit confusing, hope its clear enough, really need help on this one.
Many thanks.
Ash.
Stefi - 07 Feb 2008 12:07 GMT
Try this UDF:
Function pattsubtr(wholestring)
   partstring = ""
   For i = 3 To Len(wholestring) - 6
       If Val(Mid(wholestring, i, 7)) > 0 Then
           partstring = Mid(wholestring, i - 2, 9)
           Exit For
       End If
   Next i
   pattsubtr = partstring
End Function

There is one limitation: NNNNNNN cannot be 0000000

Regards,
Stefi



„ashg657” ezt írta:

> Complex one here I think.....
>
[quoted text clipped - 14 lines]
> Many thanks.
> Ash.
Mike H - 07 Feb 2008 12:43 GMT
Possibly with your strings in Column A

Sub RemoveLetters()
Dim NoLetterstring As String
Set myrange = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
For Each c In myrange
Numberstring = c.Value
Start = 0
For L = Len(Numberstring) To 1 Step -1
   If Not IsNumeric(Mid(Numberstring, L, 1)) Then
   If found = False Then Start = Start + 1
   Else
       found = True
       NoLetterstring = Mid(Numberstring, L, 1) & NoLetterstring
   End If
Next L
a = Len(Numberstring)
b = Len(NoLetterstring)
c.Offset(0, 1).Value = Mid(Numberstring, (a - (b + Start)) - 1, b + 2)
NoLetterstring = ""
found = False
Start = 0
Next
End Sub

Mike

> Complex one here I think.....
>
[quoted text clipped - 14 lines]
> Many thanks.
> Ash.
Ron Rosenfeld - 07 Feb 2008 12:53 GMT
>Complex one here I think.....
>
[quoted text clipped - 14 lines]
>Many thanks.
>Ash.

Here is a UDF that will do that.

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 below into the window that opens.

To use this UDF, enter the formula

=ExtractPattern(cell_ref) into some cell, where cell_ref is either a reference
to the cell containing the string, or the actual string.

=========================
Option Explicit
Function ExtractPattern(str As String)
Dim re As Object, mc As Object
Set re = CreateObject("vbscript.regexp")
re.Pattern = "[A-Z]{2}\d{7}"
Set mc = re.Execute(str)
If mc.Count >= 1 Then
   ExtractPattern = mc(0).Value
Else
   ExtractPattern = ""
End If
End Function
============================
--ron
T. Valko - 07 Feb 2008 18:54 GMT
Maybe this...

> LLNNNNNNN
> (L= Letter, N = Number)

Assuming the N portion is *always* 7 characters and there are *no other
numeric characters* in the string:

=MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))-2,9)

Signature

Biff
Microsoft Excel MVP

> Complex one here I think.....
>
[quoted text clipped - 14 lines]
> Many thanks.
> Ash.
Rick Rothstein (MVP - VB) - 07 Feb 2008 22:43 GMT
Here is my UDF solution...

Function FindLLNNNNNNN(Cel As Range)
 Dim X As Long
 If Cel.Count = 1 Then
   For X = 1 To Len(Cel.Value) - 8
     If Mid(Cel.Value, X, 9) Like "[A-Za-z][A-Za-z]#######" Then
       FindLLNNNNNNN = Mid(Cel.Value, X, 9)
       Exit Function
     End If
   Next
 End If
End Function

Rick

> Complex one here I think.....
>
[quoted text clipped - 14 lines]
> Many thanks.
> Ash.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.