循环遍历列时Excel宏"类型不匹配"错误

Bla*_*sui 0 excel vba excel-vba mismatch type-mismatch

每次调用此函数都会导致运行时错误"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
Run Code Online (Sandbox Code Playgroud)

Ale*_* K. 5

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

For Each cell In Sheet1.Columns(1).Rows '//or .cells
Run Code Online (Sandbox Code Playgroud)

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

  • + 1我还建议使用`.Find`而不是循环 (3认同)