Excelmacros“types不匹配”错误,而循环通过一列
每次调用此函数都会导致运行时错误“13”types不匹配。 为什么会这样呢?
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
正如我在评论中所build议的,这是另一种select
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
您不能从.Columns(1)
访问cell.value
,因为它返回一个包含整个列的范围,
For Each cell In Sheet1.Columns(1).Rows '//or .cells
在比赛结束后退出for循环也许是一个好主意。