【问题标题】:Suddenly runtime error 13 for no evident reason突然出现运行时错误 13,原因不明
【发布时间】:2021-05-13 17:52:52
【问题描述】:

多年来,我一直在使用修改后的匹配功能,效果很好。但是突然我无缘无故地收到运行时错误13。该函数在实际过程中调用了两次,才弹出错误。第一次一切正常,第二次出现错误。代码如下:

Public Function xMatch(ByRef Direction_Range As Range, ByVal Find_Value_Or_String, Occ_Number As Integer, Row_True_Or_Column_False As Boolean, RelativePosition_True_Or_AbsolutePosition_False As Boolean) As Integer
Dim xMTcell
toolVar1 = 0
xMatch = 0
occurrencesCount = 0
If RelativePosition_True_Or_AbsolutePosition_False = True Then
    If Row_True_Or_Column_False = True Then
        toolVar1 = Range(Split(Direction_Range.Address, ":")(0)).Row - 1
    Else
        toolVar1 = Range(Split(Direction_Range.Address, ":")(0)).Column - 1
    End If
End If
For Each xMTcell In Direction_Range
    If xMTcell.Value = Find_Value_Or_String Then
        occurrencesCount = occurrencesCount + 1
        If occurrencesCount = Occ_Number Then
            If Row_True_Or_Column_False = True Then
                xMatch = xMTcell.Row - toolVar1
            Else
                xMatch = xMTcell.Column - toolVar1
            End If
            Exit For
        End If
    End If
Next xMTcell
End Function

toolVar1 和occurrencesCount 在模块中声明。该函数可以在任何范围(Direction_Range)中搜索并找到值(Find_Value_Or_String)。与常规匹配函数相反,您可以决定 (Occ_Number) 如果该范围内有多个查找,则哪个查找将是您需要的查找。此外,您可以决定是否需要该查找的行或列,以及是否需要该行/列绝对(与工作表相比的位置)或相对(与 Direction_Range 相比的位置)。

错误出现在这一行:

If xMTcell.Value = Find_Value_Or_String Then

由于 xMTcell 是 Direction_Range 的一部分,我检查了 Range,它显然是正确的,加上它是一个范围,仅此而已。我还检查了值,它是他正在寻找的字符串,可以在该范围内手动找到。我不明白为什么它在流程的其他阶段使用完全相同类型的 Direction_Range 和 Find_Value_Or_String 可以正常工作,但突然就不行了。我已经尝试将 xMTcell 声明为 Range,但没有任何区别。

谁有想法?

问候 卡尔


根据 cmets 我做了以下检查:

Debug.Print VarType(Direction_Range)
Debug.Print Direction_Range.Address
Debug.Print VarType(xMTcell.Value)
Debug.Print VarType(Find_Value_Or_String)
Debug.Print xMTcell.Address

对于我得到的非窃听过程


8204
$A$4:$N$4
8
8
$B$4

对于窃听过程

8204
$A$5:$A$16
8204
8
$A$5:$A$16

所以它的范围有问题,为什么它的行为不同?

【问题讨论】:

  • 出错时xMTcellFind_Value_Or_String的值是多少?
  • 在您的工作表上查找 #N/A#VALUE!#REF! 错误。这些值具有Variant/Error 数据类型,每次您尝试将它们与Variant/Error 以外的任何内容进行比较时,都会引发类型不匹配。使用IsError 函数测试变体的子类型,并在子类型为Error 时返回True
  • @BigBen xMTcell 在鼠标悬停时不显示任何值(与范围一样),Find_Value_Or_String 显示字符串
  • 您可以使用Debug.Print。检查即时窗口中的输出(使用 Ctrl+G 调出)。也很有用:Debug.Print VarType(xMTcell.Value)Debug.Print VarType(Find_Value_Or_String)
  • 8204 意味着xMTcell 是一个多单元格范围,而不是单个单元格。也可以使用Debug.Print xMTcell.Address 进行验证。很可能您将RowColumn 传递给Direction_Range。可能通过使用For Each xMTcell In Direction_Range.Cells解决。

标签: excel vba runtime-error match type-mismatch


【解决方案1】:

TL;DR

For Each xMTcell In Direction_Range 更改为For Each xMTcell In Direction_Range.Cells


一些调试和一般提示(总结 cmets 和您在其中的反馈):

  • Dim xMTcell - 将其设为 Range
  • 使用Debug.PrintControl+G 调出即时窗口并检查输出)。
  • Debug.Print VarType(xMTcell.Value) 返回 8204:根据 VarType 文档,这意味着 xMTcell.ValuevbArrayvbVariants (8192 + 12 = 8204)。
  • Debug.Print VarType(Find_Value_or_String) 返回8:再次根据VarType 文档,这意味着Find_Value_or_StringString
  • 类型不匹配是因为您无法将 String 与数组进行比较。
  • xMTCell.Value 是一个数组这一事实表明xMTCell 是一个多单元格 范围,而不是单个单元格。
  • ...通过Debug.Print xMTcell.Address 的输出验证为多单元格范围。
  • 问题很可能是您将RowColumn 作为Direction_Range 传递,即您使用RowsColumns 返回一个范围。循环遍历 RowColumn 时,您需要指定循环遍历各个单元格。

【讨论】:

  • 其实这就是区别。在工作过程中,我给Direction_Range 一个Range(cells...,cells...),在非工作过程中它得到someRange.Columns(1)。除非我想在任何地方更改它,否则我怎么能告诉 For Each 函数它应该逐个单元格地传递 Range(cells...,cells...)Range.Columns(x)Direction_Range
  • For Each xMTcell In Direction_Range.Cells 不起作用吗? (也许你跳过了 TLDR)。
  • 天哪,就是这样...非常感谢!
猜你喜欢
  • 2020-06-27
  • 2015-11-08
  • 1970-01-01
  • 2014-11-04
  • 2018-12-12
  • 1970-01-01
  • 2019-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多