3

我创建了一个用户定义的函数来显示公司的匹配程度。只要我用作 thisRange 的范围小于或接近第 2800 行,此函数就非常有效。我有一个 4000 家公司的列表,当我尝试将这些公司包含在超过 2800 行中时,该函数不起作用。范围只能这么大吗?有没有办法解决这个问题?

这是代码:

Function bestMatch(company As String, thisRange As Range) As String

Dim min As Double, nextMin As Double
Dim cell As Range
Dim score As Double
Dim best As String, str As String, nextBest As String
min = 99999
nextMin = 99999

For Each cell In thisRange.Cells
    str = cell.Value
    substr1 = Left(str, 1)
    substr2 = Left(company, 1)
    score = Leven(company, str)
    If score < min And substr1 = substr2 Then
        min = score
        best = str
    ElseIf score = min And substr1 = substr2 Then
        min = score
        best = str + " && " + best
    ElseIf score > min And score <= nextMin And substr1 = substr2 Then
        nextMin = score
        nextBest = str
        min = min
        best = best
    End If
    Next

If min > 15 Then
    bestMatch = "No Match Found"
ElseIf min <= 15 Then
    bestMatch = "1. " + best + "    2. " + nextBest
End If

End Function
4

3 回答 3

2

请参阅网页Excel 规范和限制

此页面的部分包括: 工作表和工作簿规范和限制 计算规范和限制 图表规范和限制 数据透视表和数据透视图报告规范和限制 共享工作簿规范和限制

我相信一个范围可以和整个工作表一样大。工作表的大小限制为 1,048,576 行 x 16,384 列。

在“计算规范和限制”部分,选定范围的限制列为 2048。

于 2014-06-30T18:41:03.387 回答
1

如果您首先尝试转储数组中的所有值怎么办

Function bestMatch(company As String, start_cell As Range, int count) As String
    Dim vals() as Variant
    vals = start_cell.Resize(count,1).Value2

    For i=1 to count
        substr1 = Left(vals(i,1), 1)
        ...
    Next i
    ...
End Function

我已经用> 4000个细胞尝试过这个,没有问题。

于 2013-07-02T19:50:06.347 回答
0

Range 对象的子字符串有一个限制,它会根据您使用的 Excel 版本而有所不同。但据我记得,我很确定它小于 4000,不管你有什么版本。

为了扭转这件事,你应该把它分成几个部分并使用Union。

于 2013-07-02T16:09:48.913 回答