迭代范围中的单元格时键入不匹配错误

时间:2014-01-28 15:12:22

标签: excel vba excel-vba

您好我正在尝试在我的电子表格上运行以下vb代码,但我在Randge期间收到错误,表明数据类型不匹配。我只是想锁定值为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

1 个答案:

答案 0 :(得分:1)

试试这个:

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

BTW,最好将ActiveSheet更改为Worksheets("SheetName")