Excel - 根据颜色锁定单元格?

Beh*_*win 3 colors cells lock

我有这个 excel 表,我想在其中保护某些单元格免于格式化和编辑。所有这些单元格都用特定颜色着色。

该工作表非常大,因此我正在寻找一种方法来锁定所有这些单元格,然后能够在不更改我想要锁定的单元格的情况下批量格式化所有其他单元格。

有没有办法告诉excel锁定具有特定颜色的单元格?

Dav*_*ave 5

是的,使用 VBa ......只需将其复制到 Visual Basic 屏幕中的“ThisWorkbook”中,然后运行它(绿色播放三角形)

在此处输入图片说明

Sub WalkThePlank()

    dim colorIndex as Integer
    colorIndex = 3                   'UPDATE ME TO YOUR COLOUR OR BE FED TO THE SHARKS   

    Dim rng As Range

    For Each rng In ActiveSheet.UsedRange.Cells

        Dim color As Long
        color = rng.Interior.ColorIndex
        If (color = colorIndex) Then   
            rng.Locked = True
        else
            rng.Locked = false    'this will remove any locks for those not in the given color
        End If

    Next rng

End Sub
Run Code Online (Sandbox Code Playgroud)

VBa 中没有撤消功能,因此请先复制您的文件(以创建备份)!

颜色索引 - http://dmcritchie.mvps.org/excel/colors.htm

如何在 MS Office 中添加 VBA?

以上假设您没有合并的单元格并且您的工作表不受保护。

如果您不确定所需的 colorIndex 是什么,请先使用此脚本

Sub Find()

Dim colorIndexFinder As Integer
colorIndexFinder = Range("A1").Interior.colorIndex  'CHANGE A1 to the cell with the colour you want to use
MsgBox (colorIndexFinder)

End Sub
Run Code Online (Sandbox Code Playgroud)

编辑

您已经提到您确实使用了合并单元格

请尝试

Sub WalkThePlank()

Dim colorIndex As Integer
colorIndex = 3                   'UPDATE ME TO YOUR COLOUR OR BE FED TO THE SHARKS

Dim rng As Range

For Each rng In ActiveSheet.UsedRange.Cells

    Dim color As Long
    color = rng.Interior.colorIndex

    If (color = colorIndex) Then
        If (rng.MergeCells) Then
            rng.MergeArea.Locked = True
        Else
            rng.Locked = True
        End If
    Else
        If (rng.MergeCells) Then
            rng.MergeArea.Locked = False
        Else
            rng.Locked = False
        End If
    End If

    Next rng

End Sub
Run Code Online (Sandbox Code Playgroud)

  • 它是@duDE,但是,我写了我的,我没有复制和粘贴:) 当我发布我的答案时,你的答案不可见(你已经回答了它,但我必须刷新我的页面才能看到你的答案) ,我向你保证我不只是复制你的答案! (2认同)
  • 没关系,我明白了:) +1 来自我! (2认同)