fredrerik84
Active Member
- Joined
- Feb 26, 2017
- Messages
- 383
hi im trying to optimize the following code:
so far this is what I got but im stuck :/
if anyone at all have suggestion to help me speed this code up it would be much appreciated
Code:
Function VLookLike(txt As String, rng As Range) As String
Dim temp As String, e, n As Long, a()
Static RegX As Object
If RegX Is Nothing Then
Set RegX = CreateObject("VBScript.RegExp")
With RegX
.Global = True
.IgnoreCase = True
.Pattern = "(\S+).*" & Chr(2) & ".*\1"
End With
End If
With RegX
For Each e In rng.Value
If UCase$(e) = UCase(txt) Then
VLookLike = e
Exit For
End If
temp = Join$(Array(e, txt), Chr(2))
If .test(temp) Then
n = n + 1
ReDim Preserve a(1 To 2, 1 To n)
a(2, n) = e
Do While .test(temp)
a(1, n) = a(1, n) + Len(.Execute(temp)(0).submatches(0))
temp = Replace(temp, .Execute(temp)(0).submatches(0), "")
Loop
End If
Next
End With
If (VLookLike = "") * (n > 0) Then
With Application
VLookLike = .HLookup(.Max(.Index(a, 1, 0)), a, 2, False)
End With
End If
End Function
so far this is what I got but im stuck :/
Code:
Sub VLookLike()
Dim txt As String
Dim rng As Variant
Dim temp As String, e, n As Long, a()
Static RegX As Object
Dim Vlook As String
rng = Range("H" & 2 & ":H" & 1206)
txt = "Sydney United"
If RegX Is Nothing Then
Set RegX = CreateObject("VBScript.RegExp")
With RegX
.Global = True
.IgnoreCase = True
.Pattern = "(\S+).*" & Chr(2) & ".*\1"
End With
End If
With RegX
For e = LBound(rng, 1) To UBound(rng, 1)
If UCase$(e) = UCase(txt) Then
Vlook = e
Exit For
End If
temp = Join$(Array(e, txt), Chr(2))
If .test(temp) Then
n = n + 1
ReDim Preserve a(1 To 2, 1 To n)
a(2, n) = e
Do While .test(temp)
a(1, n) = a(1, n) + Len(.Execute(temp)(0).submatches(0))
temp = Replace(temp, .Execute(temp)(0).submatches(0), "")
Loop
End If
Next e
End With
If (Vlook = "") * (n > 0) Then
With Application
Vlook = .HLookup(.Max(.Index(a, 1, 0)), a, 2, False)
End With
End If
End Sub
if anyone at all have suggestion to help me speed this code up it would be much appreciated