迭代范围中的单元格时,input不匹配错误

您好我试图在我的电子表格上运行以下vb代码,但在Randge期间出现数据types不匹配的错误。 我只是试图locking值为0的单元格。有一些单元格带有#NA值任何想法?

Sub Test() Dim Cell As Range Set MyPlage = Range("J6:J1074") For Each Cell In MyPlage.Cells If Not IsError(Cell) Then If Range("J6:J1074").Value = "0" Then Range("J6:J1074").Locked = True End If End If Next End Sub 

试试这个:

 Sub Test() Dim Cell As Range Dim MyPlage As Range With ThisWorkbook.ActiveSheet .Unprotect .Cells.Locked = False Set MyPlage = .Range("J6:J1074") For Each Cell In MyPlage If Not IsError(Cell) Then If Cell.Value = "0" Then Cell.Locked = True End If End If Next .Protect End With End Sub 

顺便说一句,最好将ActiveSheet更改为Worksheets("SheetName")