Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5
Default Extracting string within a string

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.
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,646
Default Extracting string within a string

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.....

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.

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 11,501
Default Extracting string within a string

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

"ashg657" wrote:

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.

  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,651
Default Extracting string within a string

On Thu, 7 Feb 2008 03:21:06 -0800, ashg657
wrote:

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.


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
  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 15,768
Default Extracting string within a string

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&"0123456 789"))-2,9)

--
Biff
Microsoft Excel MVP


"ashg657" wrote in message
...
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.





  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1
Default Extracting string within a string

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


"ashg657" wrote in message
...
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.


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
extracting data from a string Iguss Excel Worksheet Functions 4 November 19th 07 09:50 PM
Extracting Numbers from string Keyrookie Excel Worksheet Functions 4 October 4th 07 11:47 PM
Extracting a string Peter Rooney Excel Discussion (Misc queries) 5 June 20th 06 06:34 PM
Extracting from a text string AmyTaylor Excel Worksheet Functions 3 June 24th 05 01:34 PM
extracting numbers from string Chris Dowell via OfficeKB.com Excel Discussion (Misc queries) 1 January 12th 05 09:37 PM


All times are GMT +1. The time now is 10:45 AM.

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

About Us

"It's about Microsoft Excel"