代码之家  ›  专栏  ›  技术社区  ›  Blank Chisui

在列中循环时发生Excel宏“类型不匹配”错误

  •  0
  • Blank Chisui  · 技术社区  · 13 年前

    此函数的每次调用都会导致运行时错误“13”类型不匹配。为什么会发生这种情况?

    Public Function VersionExists(versionId As String)
    
       VersionExists = False
    
       For Each cell In Tabelle2.Columns(1)
          If cell.Value = versionId Then
             VersionExists = True
          End If
       Next
    
    End Function
    
    2 回复  |  直到 13 年前
        1
  •  5
  •   Alex K.    13 年前

    您无法访问 cell.value 从…起 .Columns(1) 因为它返回一个包含整个列的范围;

    For Each cell In Sheet1.Columns(1).Rows '//or .cells
    

    也许在比赛结束后退出for循环也是个好主意。

        2
  •  2
  •   Siddharth Rout    13 年前

    这是我在评论中建议的一个替代方案

    Public Function VersionExists(versionId As String) As Boolean
        Dim aCell As Range, rng As Range
    
        Set rng = Tabelle2.Columns(1)
    
        Set aCell = rng.Find(What:=versionId, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
    
        If Not aCell Is Nothing Then VersionExists = True
    End Function