嵌套最小选择| Excel VBA自定义项参数

嵌套最小选择| Excel VBA自定义项参数,vba,user-defined-functions,Vba,User Defined Functions,Excel 2015/Access 2015 主函数:LavensteinDistance返回一个整数 Function LDCellsTwo(sR As Range, tR As Range, IDRange As Range) As Integer Dim tRNum As Integer: Dim CLVD As Integer: Dim MinLVD As Integer: Dim MinID As Integer For tRNum = 0 To tR.Count CLDV =

Excel 2015/Access 2015

主函数:LavensteinDistance返回一个整数

Function LDCellsTwo(sR As Range, tR As Range, IDRange As Range) As Integer
Dim tRNum As Integer: Dim CLVD As Integer: Dim MinLVD As Integer: Dim MinID As Integer
For tRNum = 0 To tR.Count
    CLDV = LevenshteinDistance(sR.Text, tR(tRNum))
    if IsEmpty(MinLDV) then MinLDV = CLDV + 1
    If CLDV < MinLDV Then
        MinLDV = CLDV
        MinID = IDRange(tRNum)
    End If
Next tRNum

LDCellsTwo = MinID
End Function
函数LDCellsTwo(sR作为范围,tR作为范围,IDRange作为范围)作为整数
Dim tRNum As Integer:Dim CLVD As Integer:Dim MinLVD As Integer:Dim MinID As Integer
对于tRNum=0到tR.计数
CLDV=LevenshteinDistance(sR.Text,tR(tRNum))
如果为空(MinLDV),则MinLDV=CLDV+1
如果CLDV
编辑:谢谢Scott的精彩捕捉-仍然是#NAME?错误

目标:让LDCellsTwo函数通过迭代
t
范围内的各个项目,与
sR
范围内的单个值(其中
ID
范围和
t
范围的长度隐式相同)来返回与第一个最低LevenshTeInstance值相关的ID值


错误:
#NAME?
返回到单元格中

不确定为什么这会造成如此大的影响,但是

Function LDCellsTwo(sR As Range, tR As Range, IDRange As Range) As Integer
Dim tRNum As Integer: Dim CLVD As Integer: Dim MinLVD As Integer: Dim MinID As Integer
For tRNum = 1 To tR.Count
    CLDV = LevenshteinDistance(sR.Text, tR(tRNum))
    if IsEmpty(MinLDV) then MinLDV = CLDV + 1
    If CLDV < MinLDV Then
        MinLDV = CLDV
        MinID = IDRange(tRNum)
    End If
Next tRNum

LDCellsTwo = MinID
End Function
函数LDCellsTwo(sR作为范围,tR作为范围,IDRange作为范围)作为整数
Dim tRNum As Integer:Dim CLVD As Integer:Dim MinLVD As Integer:Dim MinID As Integer
对于tRNum=1到tR.计数
CLDV=LevenshteinDistance(sR.Text,tR(tRNum))
如果为空(MinLDV),则MinLDV=CLDV+1
如果CLDV
变化是:

从tRNum=
0
到tR.Count


对于tRNum=
1
到tR.Count

您的函数是在常规模块中定义的,还是在工作表模块中定义的?应该在常规模块中。@TimWilliams,是的,它在常规模块中定义。我实际上只对变更事件使用工作表模块。谢谢。由于
t
ID
已经是范围,您不必使用
Range()
函数调用它们,只需使用
t
ID
。比如:
tr=t
。在每次引用数组之后,也删除
()
;注意:您应该在赋值之前声明这些。最后你也没有返回任何东西。@ScottCraner,Yikes是的,捕捉得很好——我能说这是漫长的一天吗?我仍然收到相同的错误,但我已经实现了您在编辑的问题中的建议您是否在您的for trNum行上设置了一个断点并逐步完成了代码?很抱歉,我错过了,这是因为您更改为遍历一个范围,并且tR(0)处没有值,它从1开始。