Function CleanTextByKeys$(ByVal Texts, ByVal Keys, _
Optional ByVal CleanDown As Boolean = False, _
Optional ByVal Calculate As Boolean = False)
If Calculate Then Application.Volatile
Static RE As Object
Dim S$, Key, Text
If VBA.IsArray(Keys) Then
For Each Key In Keys
If CStr(Key) <> "" Then
S = S & VBA.IIf(S = "", "", "|") & CStr(Key)
End If
Next
Else
S = CStr(Keys)
End If
If VBA.TypeName(Texts) = "Range" Then
Text = Texts(1, 1).Value2
Else
Text = CStr(Texts): CleanDown = False
End If
If RE Is Nothing Then
Set RE = VBA.Interaction.CreateObject("VBScript.RegExp")
RE.Global = True
RE.MultiLine = True
RE.IgnoreCase = False
End If
With RE
.Pattern = "\d+" & VBA.IIf(S = "", "", "|") & S
If Not .test(Text) Then Exit Function
Dim M, Ms, Tmp$
Set Ms = .Execute(Text)
For Each M In Ms
Tmp = Tmp & VBA.IIf(Tmp = "", "", " ") & M
Next
End With
CleanTextByKeys = Tmp
If CleanDown Then
With Application
.Evaluate "Callback_CleanTextByKeys('[" & _
Texts(2, 1).Parent.Parent.Name & "]" & Texts(2, 1).Parent.Name & "'!" & Texts(2, 1).Address(0, 0) & _
", """ & S & """, '[" & .Caller.Parent.Parent.Name & "]" & .Caller.Parent.Name & "'!" & .Caller.Offset(1).Address(0, 0) & ")"
End With
End If
End Function
Sub Callback_CleanTextByKeys(CTBK_Range As Range, Keys, CTBK_Caller As Range)
If CTBK_Caller Is Nothing Then Exit Sub
Dim LR&, cLR&, I&, Total(), Arr, B As Boolean
LR = CTBK_Range(Rows.Count - CTBK_Range.Row).End(3).Row - CTBK_Range.Row + 1
If LR <= 0 Then Exit Sub
B = Application.ScreenUpdating
Application.ScreenUpdating = False
Arr = CTBK_Range.Resize(LR).Value2
ReDim Total(1 To LR, 1 To 1)
For I = 1 To LR
If Arr(I, 1) <> "" Then _
Total(I, 1) = CleanTextByKeys(Arr(I, 1), Keys)
Next
CTBK_Caller(1, 1).Resize(LR).Value = Total
cLR = CTBK_Caller(Rows.Count - CTBK_Caller.Row).End(3).Row - CTBK_Caller.Row + 1
If cLR - LR > 0 Then _
CTBK_Caller(LR + 1, 1).Resize(cLR - LR).ClearContents
Application.ScreenUpdating = B
End Sub