I'm not quite sure what you're asking for... if you want the UDF to accept multiple strings for comparative purposes then perhaps the below adaptation might help ?
Function CompareString(rngS1 As Range, rngS2 As Range, strType As String, Optional boolCase As Boolean = True) As Variant
Dim vW1, vW2
Dim oDic As Object
Dim lngW As Long, lngU As Long, lngM As Long, lngTemp As Long, rngCell As Range
Dim strTemp As String, strB As String
vW2 = Split(rngS2.Text, " ")
Set oDic = CreateObject("Scripting.Dictionary")
For lngW = LBound(vW2) To UBound(vW2) Step 1
strTemp = Replace(vW2(lngW), ".", "")
With oDic
If Not .exists(strTemp) Then
lngU = lngU + 1
.Add strTemp, lngU
End If
End With
Next lngW
Set oDic = Nothing
For Each rngCell In rngS1.Cells
If rngCell.Value <> "" Then
If rngCell.Value = strTemp Then
lngM = lngU
strB = strTemp
Else
vW1 = Split(rngCell.Text, " ")
lngTemp = 0
For lngW = LBound(vW2) To UBound(vW2) Step 1
strTemp = vW2(lngW)
If boolCase Then
lngTemp = lngTemp + rngS1.Parent.Evaluate("SUMPRODUCT(--ISNUMBER(FIND("" " & strTemp & " "","" ""&SUBSTITUTE(" & rngCell.Address & ",""."","""")&"" "")))")
Else
lngTemp = lngTemp - IsNumeric(Application.Match(strTemp, vW1, 0))
End If
Next lngW
If lngTemp > lngM Then
lngM = lngTemp
strB = rngCell.Text
End If
End If
End If
Next rngCell
Select Case UCase(strType)
Case "P"
CompareString = lngM / lngU
Case "S"
CompareString = strB
End Select
End Function
the above is now called along the lines of:
C1: =COMPARESTRING($A$1:$A$4,$B$1,"P")
to return the greatest percentage
D1: =COMPARESTRING($A$1:$A$4,$B$1,"S")
to return the associated string
In the case of matching "max" % the first string is returned and as such the above code would stop should an exact match be found - ie it won't iterate through additional cells unnecessarily.
As before you have the optional Boolean as final parameter to denote case sensitivity (default is TRUE - ie Case Sensitive matching)
(edit: and apologies for slight delay in responding - I tend to pop in and out of the forum in the afternoons)
Bookmarks