【问题标题】:Why am I getting Error 2042 in VBA Match?为什么我在 VBA 匹配中收到错误 2042?
【发布时间】:2013-03-09 17:43:26
【问题描述】:

我有 A 列:

+--+--------+
|  |  A     |
+--+--------+
| 1|123456  |
|--+--------+
| 2|Order_No|
|--+--------+
| 3|    7   |
+--+--------+

现在如果我输入:

=Match(7,A1:A5,0)

进入我得到的工作表上的一个单元格

3

结果。 (这是需要的)

但是当我进入这一行时:

Dim CurrentShipment As Integer
CurrentShipment = 7
CurrentRow = Application.Match(CurrentShipment, Range("A1:A5"), 0)

CurrentRow 的值是“错误 2042”

我的第一个直觉是确保值 7 实际上在范围内,并且确实如此。

我的下一个可能是 Match 函数需要一个字符串,所以我尝试了

Dim CurrentShipment As Integer
CurrentShipment = 7
CurrentRow = Application.Match(Cstr(CurrentShipment), Range("A1:A5"), 0)

无济于事。

【问题讨论】:

  • 您的代码按原样为我工作。您收到的错误消息相当于#N/A。也许它有时专注于错误的工作表?您可以尝试在运行代码之前主动选择正确的工作表。

标签: excel excel-2007 vba


【解决方案1】:

附带说明一下,对于将来遇到此错误的任何人,任何函数都返回可能的错误,变体类型工作得很好:

Dim vreturn as variant 

vreturn = Application.Match(CurrentShipment, Range("A1:A5"), 0) ' this could be any function like a vlookup for example as well

If IsError(vreturn) Then
    ' handle error
Else
    CurrentRow = cint(vreturn)
End If

【讨论】:

    【解决方案2】:

    查看VBA列表Cell Error Values:

    Constant    Error number  Cell error value
    xlErrDiv0   2007          #DIV/0!
    xlErrNA     2042          #N/A
    xlErrName   2029          #NAME?
    xlErrNull   2000          #NULL!
    xlErrNum    2036          #NUM!
    xlErrRef    2023          #REF!
    xlErrValue  2015          #VALUE!
    

    尝试将CurrentShipment 的值从Integer 转换为Long 而不是String

    CurrentRow = Application.Match(CLng(CurrentShipment), Range("A1:A5"), 0)
    

    【讨论】:

    • 它适用于整数,但不适用于小数,也不适用于字符串,所以感觉有点狡猾......
    • 这也适用于我匹配日期的数据。我真的不知道为什么会这样,因为 CLng 正在将值转换为长整数。进一步的解释表示赞赏。
    【解决方案3】:

    如果您在对象浏览器中查找 match 函数,它会返回 double,因此我已将变量 CurrentRow 声明为 double 并且它接受 3 个变体参数。如果它适合您,请尝试以下代码。

      Sub sample()
    
        Dim CurrentShipment As Variant
        CurrentShipment = 7
    
        Dim CurrentRow As Double
        CurrentRow = Application.Match(CurrentShipment, Range("A1:A5"), 0)
        End Sub
    

    【讨论】:

      【解决方案4】:

      对我来说,它工作得很好,没有任何类型的转换。我两个都用过:

      Application.WorksheetFunction.Match(CurrentShipment, Range("A1:A5"), 0)

      Application.Match(CurrentShipment, Range("A1:A5"), 0)

      将 CurrentShipment 标注为 Integer、Double、Long 或 Variant,一切正常...

      【讨论】:

        【解决方案5】:

        有趣的是,我将您的数据输入到一张空白的 Excel 表中,然后运行您原来的 sn-p 代码。它按预期返回 3,而无需将 CurrentShipment 转换为 String 或 Long。

        Not DIM'ing CurrentRow 默认情况下使其成为 Variant,但即使将它们都设置为 Integer 或 CurrentRow 为 Byte 也不会引发错误,因此使用 Double 作为返回类型是多余的。

        Sub Match()
        
        Dim CurrentShipment As Integer
        Dim CurrentRow As Byte '<--- NOTE
        
        CurrentShipment = 7
        CurrentRow = Application.Match(CurrentShipment, Range("A1:A5"), 0)
        
        MsgBox CurrentRow
        
        End Sub
        

        【讨论】:

          【解决方案6】:

          尝试在具有多个工作表的工作簿中使用匹配功能时,我收到了完全相同的错误消息。 如果我们尝试匹配的主题不存在,则对接收结果的变量使用变体类型可以很好地避免运行错误。 但我的错误在于使用范围的位置。指向具有要搜索范围的工作表至关重要。首先,我在没有工作表引用的情况下使用 Range(...),并且只有在激活该工作表时该函数才能正常工作。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-06-29
            • 1970-01-01
            • 1970-01-01
            • 2019-03-18
            • 1970-01-01
            • 1970-01-01
            • 2013-04-22
            相关资源
            最近更新 更多