Sub test()
Dim a(1), b, x, i As Long
x = Range("a1", Range("a" & Rows.Count).End(xlUp)).Address
a(0) = Evaluate("index(replace(trim(" & x & "),1,find("" ""," & x & "),""""),,)")
ReDim b(1 To UBound(a(0), 1), 1 To 1)
a(1) = Range("e1", Range("e" & Rows.Count).End(xlUp)).Resize(, 2)
With CreateObject("VBScript.RegExp")
For i = 1 To UBound(a(1), 1)
a(1)(i, 2) = ""
.Pattern = " *(\d+) *(ctn|bags?)$"
If .test(a(1)(i, 1)) Then a(1)(i, 2) = .Execute(a(1)(i, 1))(0).submatches(0) & " CRT"
a(1)(i, 1) = .Replace(a(1)(i, 1), "")
.Pattern = "(\d)(\D)"
a(1)(i, 1) = Application.Trim(.Replace(a(1)(i, 1), "$1 $2"))
Next
End With
For i = 1 To UBound(a(0), 1)
b(i, 1) = VLookLike(a(0)(i, 1), a(1))
Next
[c1].Resize(UBound(b, 1)) = b
End Sub
Function VLookLike(s, a) As String
Dim i As Long, ii As Long, x(2), b(), n As Long, flg As Boolean
x(0) = Split("^" & Replace(Application.Trim(s), " ", "^ ^") & "^")
For i = 1 To UBound(a, 1)
'If i = 28 Then Stop
If UCase$(s) = UCase(Application.Trim(a(i, 1))) Then
VLookLike = a(i, 2): Exit Function
End If
x(1) = Split("^" & Replace(Application.Trim(a(i, 1)), " ", "^ ^") & "^")
For ii = 0 To UBound(x(0))
x(1) = Filter(x(1), x(0)(ii), False, 1)
If UBound(x(1)) = -1 Then VLookLike = a(i, 2): flg = True: Exit For
Next
If Not flg Then
x(2) = UBound(Split("^" & Replace(Application.Trim(a(i, 1)), " ", "^ ^") & "^")) - UBound(x(1))
If x(2) Then
n = n + 1: ReDim Preserve b(1 To 2, 1 To n): b(2, n) = a(i, 2): b(1, n) = x(2)
End If
Else
Exit For
End If
flg = False
Next
If Not flg Then
If n Then
x(0) = Application.Max(Application.Index(b, 1, 0))
VLookLike = b(2, Application.Match(x(0), Application.Index(b, 1, 0), 0))
End If
End If
End Function
Bookmarks