Extract text plus "n" characters or date on partial match
On 21 Mar 2007 05:02:45 -0700, "FabZ" wrote:
Hi everybody,
Here there are two string samples of my cells in columnA:
der. husky b/n f 4 aa. rmd 2161 huma - p.p. adopted 10.02.04
dobermann n/f m. steril+rmd24378 faruk - p.p. trasf. palombara 13/1/05
I need to extract:
rmd 2161
rmd24378
and I started from function extracteMailAddress() found on this
newsgroup
Sub extractTattoo()
sstr = Range("A4").Text
sstr = ActiveCell.Text
p = InStr(1, sstr, "rm") - 1
Do While char < " " And p 0
char = Mid(sstr, p, 1)
Debug.Print "'" & char & "'"
p = p - 1
Loop
'Get tattoo address
If p 0 Then
p = p + 1
tattoo = Mid(sstr, p, 9)
ActiveCell.Offset(0, 9).Value = tattoo
Debug.Print tatuaggio
End If
End Sub
On some cell it works, but, really, I can't say this code works and
anyway I tried also to make it work on all the column range with no
results ...
Maybe starting from the same code I need to extract dates, first,
looking for partial text match(for ex. looking for "adopted" or "-
adop." or "ado." with "*ado*") and then fill one of two cells in
different columns on the same row, always, formatting date with "dd/mm/
yyyy".
I think I need help, I'm a newbie and these codes are just a little
bit hard for my actual knowledges...
Any help would be really appreciated!
Thanks
FabZ
I don't understand your references to dates when you write that you want to
extract the "rmd" strings.
To extract the rmd strings as you write above, you could use this "regular
expression" routine:
=======================================
Option Explicit
Sub ExtractTattoo()
Dim sStr
Dim i As Long
sStr = Array("der. husky b/n f 4 aa. rmd 2161 huma - p.p. adopted 10.02.04", _
"dobermann n/f m. steril+rmd24378 faruk - p.p. trasf. palombara
13/1/05")
Dim oRegExp As Object
Dim colMatches As Object
Const sPattern As String = "rmd\s?\d+"
Set oRegExp = CreateObject("VBScript.RegExp")
With oRegExp
.IgnoreCase = True
.Global = True
.Pattern = sPattern
For i = 0 To UBound(sStr)
If oRegExp.Test(sStr(i)) = True Then
Set colMatches = oRegExp.Execute(sStr(i))
Debug.Print i, colMatches(0)
End If
Next i
End With
End Sub
=====================================
0 rmd 2161
1 rmd24378
=====================================
If you want to extract the rmd strings and the dates, you could try this
similar routine, which assumes the dates are always at the end:
=======================================
Sub ExtractTattoo()
Dim sStr
Dim i As Long
sStr = Array("der. husky b/n f 4 aa. rmd 2161 huma - p.p. adopted 10.02.04", _
"dobermann n/f m. steril+rmd24378 faruk - p.p. trasf. palombara
13/1/05")
Dim oRegExp As Object
Dim colMatches As Object
Const sPattern As String = "(rmd\s?\d+)[\s\S]+(\s\S+$)"
Set oRegExp = CreateObject("VBScript.RegExp")
With oRegExp
.IgnoreCase = True
.Global = True
.Pattern = sPattern
For i = 0 To UBound(sStr)
If oRegExp.Test(sStr(i)) = True Then
Set colMatches = oRegExp.Execute(sStr(i))
Debug.Print i, colMatches(0).submatches(0),
colMatches(0).submatches(1)
End If
Next i
End With
End Sub
============================================
0 rmd 2161 10.02.04
1 rmd24378 13/1/05
=============================================
The "work" is done by the Pattern (sPattern).
In the first case
"rmd\s?\d+"
says look for a pattern starting with
"rmd" then
an optional <space then
all of the following digits.
In the second
"(rmd\s?\d+)[\s\S]+(\s\S+$)"
The parentheses enclose "submatches", so the first submatch will be the same
"rmd" string as before.
The code then accepts all characters and newlines (spaces and non-spaces) until
it gets to the second set of parentheses which is looking for a substring that
starts with a <space
is followed by consecutive <non-space's and then by
the End of the string.
--ron
|