View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Ardus Petus
 
Posts: n/a
Default Searching for codes in text strings

In a module, paste following code:
'-------
Dim re As RegExp

Sub initre()
Set re = New RegExp
re.Pattern = "([A-Z0-9]{12}[lhc])"
re.Global = True
re.IgnoreCase = False
End Sub

Sub FindAndStoreStrings()
Dim i As Long
Dim rSearchArea As Range
Dim rSearch As Range
Dim rDestArea As Range
Dim mc As MatchCollection
Set rSearchArea = Worksheets("Sheet1").Range("A1:A8")
Set rDestArea = Worksheets("Sheet1").Range("B1")
For Each rSearch In rSearchArea
Set mc = re.Execute(rSearch.Text)
For i = 0 To mc.Count - 1
rDestArea.Value = mc(i).Value
Set rDestArea = rDestArea.Offset(1, 0)
Next i
Next rSearch
End Sub
'---------

In ThisWorkBook code, paste the following
'-----------
Private Sub Workbook_Open()
Call initre
End Sub
'---------

Run macro FindAndStoreStrings

HTH
--
AP


"Bhupinder Rayat" a écrit dans le
message de ...
Hi All,

I have text similar to the following, which is an in-house language.
----------------------------------------------
field b831 B831 write AASLQ0300000l;

PCPACIMTAAABl [B29 ]

field B7 b7 ;
field a8 @latestdate("PCP2EHSEAAAAh", jEnd);
field b8 @if(a8 <jStart, @latest("PCP2EHSEAAAAh", jEnd),
@avg("PCP2EHSEAAAAh", jStart, jEnd));
--------------------------------------------------------

I want to look through this text and copy out any 13 character codes that
are present (e.g. "PCPACIMTAAABl" ,2 "PCP2EHSEAAAAh").

These codes all share the following characteristics,

1) they are all 13 characters in length
2) the last character in the code is always either a "l", "h" or a "c".
3) they contrain no spaces
4) the first 12 characters are always in CAPS (followed by a lower "l",

"h"
or a "c".

Any help at all will be much appreciated. If you need more explanation,
please ask and I will be happily explain things further.

Regards,

Bhupinder.