如果colorindex = 0,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 

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

因为空白格式是-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