【问题标题】:Locking "visible" conditionally formatted cells within a named range锁定命名范围内的“可见”有条件格式的单元格
【发布时间】:2018-03-30 01:47:08
【问题描述】:

概述

我正在尝试锁定具有可见条件格式的命名范围内的单元格。下面的 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.

The row will change to the blue fill if criteria in column A is met, i.e. if the corresponding row in column A has the letter "f" as input.

总之,我正在尝试用 visible 蓝色填充在命名范围内以及 rest 锁定行工作表。而且,我只想编辑命名范围内没有可见蓝色填充条件格式的单元格。

我的解决方案(到目前为止)...

这个宏创建了上面提到的命名范围,并在 LockCells() 宏中使用(在这个代码 sn-p 的下面):

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

此宏循环遍历命名范围(table)中的单元格并尝试锁定可见蓝色条件命名范围内的格式化行:

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

问题

我被卡住了,因为没有锁定 可见 蓝色填充的单元格,并保持其他单元格解锁以进行编辑,在命名范围 table em> 它会锁定所有单元格。请注意,我确实希望命名范围之外的工作表的其余部分被锁定和保护。我知道这是因为条件格式应用于命名范围并评估为真。这就是为什么它将所有单元格锁定在命名范围内的原因。我关于解决这个问题的问题如下。

问题

有条件格式化单元格的状态(或可见性)属性吗?

我在想如果有这样的属性,我可以在我的 LockCells() 宏的 if 语句中使用它。例如。 如果 (color = color_index) & [条件格式可见] 那么...

非常感谢您的帮助。

谢谢。 :)

【问题讨论】:

标签: vba excel


【解决方案1】:

这是一个简化的示例。我有一个格式条件恰好是 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

条件格式规则:

表格:

参考:

http://www.excelfox.com/forum/showthread.php/338-Get-Displayed-Cell-Color-(whether-from-Conditional-Formatting-or-not)

【讨论】:

    猜你喜欢
    • 2019-06-21
    • 2020-05-19
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多