我正在尝试将范围的第n个相似之处与文本进行比较。 Similarity
函数用于返回2个文本之间的百分比相似度。
Function SimilarText(ByVal CompareText As String, _
ByRef TargetCompare As Range, _
Optional ByVal RankSimilarity As Integer = 1) As Long
Dim compareResults() As Long
ReDim compareResults(RankSimilarity)
Dim simiResult As Single
Dim smallestIndex As Integer
Dim result As String
Debug.Print (CompareText)
For Each cell In TargetCompare
simiResult = Similarity(cell.Value, CompareText)
Debug.Print (simiResult)
If simiResult > Application.Min(compareResults) Then
smallestIndex = Application.Match(Application.Min(compareResults), compareResults, 0) - 1
Debug.Print ("Index:" & smallestIndex)
compareResults(smallestIndex) = CLng(simiResult)'//This doesnt seem to do anything. I tried without the conversion but still nothing.
Debug.Print ("Smallest after update:" & compareResults(smallestIndex)) '//This always 0
End If
Next cell
SimilarText = Application.Min(compareResults) '//So this is also alway 0
End Function
我希望数组元素在每个像'(0.22,0.44)这样的单元格之后都会更新,但结果似乎总是为0。
答案 0 :(得分:0)
您的数组compareResults()被定义为Long
,并且仅用于整数。
将数据类型更改为允许使用小数的内容。
此外,在compareResults(smallestIndex) = CLng(simiResult)
行中,您强制数字成为长整数。使用其他格式,否则它将保存一个长整数。