-2

I use this formula:

=IFERROR(IF(MATCH(transf(E7);transf(Sheet2!$C$2:$C$66648);0)>0;"YES");"no")

transf is a UDF which simply converts/transforms the actual text value in cell to lowercase and does some other stuff (not the subject for this question).

Normally, if the value of transf(E7) is found in the formed array transf(Sheet2!$C$2:$C$66648), the formula returns YES, if no - no.

The UDF itself works well, it is tested many times. The problem is that this time it does not work. The returned answer is no, and it's not correct. Is this formula failure related to the big array of 66k+ items? What are the limitations of UDFs used as array formulas?

EDIT 1

This is the simplified version of my UDF:

Public Function transf(ByVal vText As Variant) As Variant
Dim aText() As Variant
Dim j As Long
    On Error GoTo ErrH

    If TypeName(vText) = "String" Then
        '...some code...
    ElseIf TypeName(vText) = "Variant()" Then
        '...some code...
    ElseIf TypeName(vText) = "Range" Then   ' <<< both instances of the UDF fall here
        ReDim aText(1 To vText.Count)
        For j = 1 To vText.Count
            aText(j) = Trim(LCase(vText(j)))
        Next
        transf = Application.Transpose(aText)   ' <<< this line causes an error 13
    Else
        transf = CVErr(xlErrValue)
    End If
ErrH:
    'This was created not for the current case, but the error the UDF gets has the same #
    If Err.Number = 13 Then
        Err.Clear
        Resume Next
    End If

End Function

If you notice any other imperfections in case of performance, please, let me know.

EDIT 2

I'm using Excel 2010. No compatibility mode, the file itself - .xlsm, the UDF is in .xlam add-in file.

4

1 回答 1

2

Transpose函数有一个限制,具体取决于您使用的 Excel 版本。在大多数情况下,它等于工作表中可能的列数——尽管在最新版本中它可能更大(我没有检查过)。

我建议您进行填充aText,这样您就不必进行转置。虽然,仅仅为了在数组中找到一个值,你根本不需要转置它。当然,您可能正在对结果做其他事情。

例如:对于垂直数组:

ReDim aText(1 To vText.Count, 1 to 1)
    For j = 1 To vText.Count
        aText(j,1) = Trim(LCase(vText(j)))
    Next

编辑:在与 ZygD 讨论和一些测试之后,将工作表函数应用于变体数组似乎还有另一个限制。除了上面提到的 TRANSPOSE 限制之外,第一维中似乎存在 65536 个元素的大小限制,如下面的代码所示。

我们用数字填充范围A:A。然后我们生成这个范围的变体数组,它要么包含整个列,要么只包含 65,536 个元素。从输出可以看出,在整列的情况下,使用 Range 对象 as lookup_array,宏按预期执行;但是如果使用变体数组作为“lookup_array”,如果它大于 65,536 个元素,则会导致错误。

使用INDEX函数也会出现类似的错误;我没有测试过其他人。


Option Explicit
Sub Tester()
    TestArr Range("A1:A65536")
    TestArr Range("A1:A65537")
End Sub

Sub TestArr(R As Range)
    Dim V

V = R

On Error Resume Next
    Debug.Print "Array Size: " & R.Rows.Count
    Debug.Print "Variant Array: " & WorksheetFunction.Index(V, 147, 1)
    If Err.Number <> 0 Then
        Debug.Print "Variant array: Error " & Err.Number, Err.Description
        Err.Clear
    End If

Debug.Print "Range Array: " & WorksheetFunction.Index(R, 147, 1)
Debug.Print vbLf

End Sub

于 2015-03-23T12:16:37.447 回答