View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Claus Busch Claus Busch is offline
external usenet poster
 
Posts: 3,872
Default VBA- Finding texts built from regex patterns

Hi,

Am Sat, 4 Mar 2017 03:53:52 -0800 (PST) schrieb stainless:

I need to recognise patterns in text starting with a specific variable text value and ending in 0-3 numeric values.

For example:

I have several strings of text that I am searching for at the start of a text string:

S

FDC

PP

Doctor


try:

Sub Test()
Dim re As Object
Dim ptrn As String
Dim Matches
Dim rngC As Range
Dim varSearch As Variant
Dim i As Long

varSearch = Range("Binder")
Set re = CreateObject("vbscript.regexp")
'Modify the range with your strings
For Each rngC In Range("A1:A10")
For i = LBound(varSearch) To UBound(varSearch)
ptrn = "^" & varSearch(i, 1) & "\d{0,2}$"
re.Pattern = ptrn
re.IgnoreCase = False
re.Global = True
Set Matches = re.Execute(rngC)
If Matches.Count 0 Then
rngC = Replace(rngC, varSearch(i, 1), varSearch(i, 2))
Exit For
End If
Next
Next
End Sub


Regards
Claus B.
--
Windows10
Office 2016