0

每次调用此函数都会导致运行时错误“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
4

2 回答 2

5

您无法访问cell.valuefrom .Columns(1),因为它返回包含整个列的范围;

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

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

于 2012-09-25T14:24:40.123 回答
2

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

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
于 2012-09-25T14:32:30.777 回答