如果colorindex = 0,则VBA解锁范围中的单元格

时间:2016-12-28 19:38:49

标签: excel vba excel-vba

如果没有背景颜色,我正在尝试解锁给定范围内的单元格。

Sub macunlock()
Dim rng1 As Range
Set rng1 = Range("A1:C1")
For Each cell In rng1
If cell.Interior.ColorIndex = 0 Then cell.Locked = False
Next cell
End Sub

但是,指定的单元格无法解锁。

1 个答案:

答案 0 :(得分:2)

因为,空白格式为-4142而非0

更改为:

If cell.Interior.ColorIndex = -4142 Then cell.Locked = False

健全的方式:

Sub macunlock()
    Dim rng1 As Range
    Set rng1 = Range("A1:C1")
    For Each cell In rng1
        If cell.Interior.ColorIndex = XlColorIndex.xlColorIndexNone Then
            cell.Locked = False
        End If
    Next cell
End Sub