概观
我正在尝试锁定具有可见条件格式的命名范围内的单元格。下面的3个链接图片将说明我的约束:
Name range: table (cells C1:E6) is set to be conditionally formatted with a blue fill color.
This is the conditional format fill color (color index #: 37) being used.
总之,我试图在命名范围内以及工作表的其余部分锁定具有可见蓝色填充的行。而且,我只想编辑指定范围内没有可见蓝色填充条件格式的单元格。
我的解决方案(到目前为止)......
此宏创建上面提到的命名范围并在LockCells()宏中使用(在此代码片段下面):
Sub NameRange()
'Create named range
Dim rng As Range
Dim range_name As String
Dim cells As String
Dim wkst As String
'Target worksheet
wkst = "Sheet1"
'Range of cells
range_name = "table"
cells = "C1:E6"
'Creates named range
Set rng = Worksheets(wkst).Range(cells)
ThisWorkbook.Names.Add Name:=range_name, RefersTo:=rng
End Sub
此宏循环遍历命名范围(表)中的单元格,并尝试锁定命名范围中的可见蓝色条件格式化行:
Sub LockCells()
'Loop through cells in a given named range
'and lock cells based on blue fill color
Dim cell As Range
Dim color_index As Integer
'Target fill color
color_index = 37
'Target worksheet to protect
wkst = "Sheet1"
'Loop through cells in named range
For Each cell In Range("table")
Dim color As Long
color = cell.FormatConditions(1).Interior.ColorIndex
If (color = color_index) Then
cell.Locked = False
Else
cell.Locked = True
End If
Next
Sets protection for worksheet
Worksheets(wkst).Protect
End Sub
问题
我被卡住了,因为它没有锁定可见的蓝色填充单元格,而是保持其他单元格未锁定以进行编辑,在命名范围表中它锁定了所有单元格。请注意,我确实希望锁定和保护命名范围之外的其余工作表。我知道这是因为条件格式应用于命名范围并评估为true。这就是它锁定命名范围内所有单元格的原因。我关于解决这个问题的问题如下。
题
条件格式化的单元格是否存在状态(或可见性)属性?
我在想是否有这样的属性,我可以在我的LockCells()宏的if语句中使用它。例如。如果(color = color_index)和[条件格式可见]那么......
非常感谢您的帮助。
谢谢。 :)
这是一个简单的例子。我有一个格式条件恰好是Cell Value = 2所以我可以通过.FormatConditions(1)
引用该规则而且规则是“=”我可以使用我的比较。您可能希望适应您正在使用的公式。
码:
Sub test()
Dim curr As Range
ActiveSheet.Cells.Locked = False
For Each curr In ActiveSheet.Range("C1:E6")
With curr.FormatConditions(1)
If curr.Value = Evaluate(.Formula1) Then curr.Locked = True
End With
Next curr
For Each curr In ActiveSheet.Range("C1:E6")
Debug.Print curr.Address & " locked = " & curr.Locked
Next curr
End Sub
条件格式规则:
片:
参考: