Hello all, i am quite new to excel programming, there is a UDF that is quite useful to me and i have been figuring out for quite some time nw, manage to understand some part of the code, but still can't seem to manage to understand the big picture of how the function should be use on. Can someone please explain to me or give me a example on what kind of excel data arrangement this UDF is used for?? Thanks
Public Function VFuzzyLookup_Phrase(Lookup_Phrase As String, Table_Array As Variant, Optional Col_Index_Num As Integer = 1)
Application.Volatile False
Dim dblBestMatch As Double
Dim iRowBest As Integer
Dim dblMatch As Double
Dim iRow As Integer
Dim strTest As String
Dim strInput As String
Dim iStartCol As Integer
Dim iEndCol As Integer
Dim iOffset As Integer
If InStr(TypeName(Table_Array), "(") + InStr(1, TypeName(Table_Array), "Range", vbTextCompare) < 1 Then
'Table_Array is not an array
VFuzzyLookup_Phrase = "ERROR"
Exit Function
End If
If InStr(1, TypeName(Table_Array), "Range", vbTextCompare) > 0 Then
Table_Array = Table_Array.Value
End If
' If you get a subscript-out-of-bounds error here, you're using a vector instead
' of the 2-dimensional array that is the default 'Value' property of an Excel range.
iStartCol = LBound(Table_Array, 2)
iEndCol = UBound(Table_Array, 2)
iOffset = 1 - iStartCol
Col_Index_Num = Col_Index_Num - iOffset
If Col_Index_Num > iEndCol Or Col_Index_Num < iStartCol Then
'Out-of-bounds
VFuzzyLookup_Phrase = "ERROR2"
Exit Function
End If
strInput = UCase(Lookup_Phrase)
iRowBest = -1
dblBestMatch = 0
For iRow = LBound(Table_Array, 1) To UBound(Table_Array, 1)
strTest = ""
strTest = Table_Array(iRow, iStartCol)
dblMatch = 0
'dblMatch = MatchPhrase(strInput, strTest) ' Consider coding up a MatchPhrase_Express() function, with the preprocessing
' (StripChars, Split) of strInput done here, rather than repeatedly
If dblMatch = 1 Then ' Bail out on finding an exact match
iRowBest = iRow
Exit For
End If
If dblMatch > dblBestMatch Then
dblBestMatch = dblMatch
iRowBest = iRow
End If
Next iRow
If iRowBest = -1 Then
VFuzzyLookup_Phrase = "#NO MATCH"
Exit Function
End If
VFuzzyLookup_Phrase = Table_Array(iRowBest, Col_Index_Num)
End Function
Bookmarks