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

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

我有一张 Excel 表,我想保护其中的一些单元格不被格式化和编辑。所有这些单元格都用特定的颜色着色。

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

有没有什么方法可以告诉 Excel 用特定的颜色锁定单元格?

答案1

是的,使用 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

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

编辑

您曾提到您确实使用合并单元格

请尝试

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

答案2

我已经发现通过使用简单的宏:

选择整个工作表(Ctrl+A)并解锁所有单元格,然后使用此宏将彩色单元格设置为再次锁定:

Dim c As Object 
For Each c In selection 
    If c.ColorIndex = 6 ' 6 is for Yellow - change to the colour you want
    c.Locked = True 
End If 
Next c 

答案3

Vba 解决方案(如何在 MS Office 中添加 VBA?

Sub LockOnlyCellsWithCertainColor()
    'Change to your color
    Const colorToLock = 65535

    Dim currentCell As Range

    ActiveSheet.Cells.Locked = False

    For Each currentCell In ActiveSheet.UsedRange.Cells
        If currentCell.Interior.Color = colorToLock Then
            If currentCell.MergeCells Then
                currentCell.MergeArea.Locked = True
            Else
                currentCell.Locked = True
            End If
        End If
    Next

End Sub

Sub GetBackgroundColorOfActiveCell()
    Debug.Print ActiveCell.Interior.Color
    MsgBox ActiveCell.Interior.Color
End Sub

答案4

只要您先取消对工作表的保护,下面的方法就适用于我,颜色索引设置为 6 表示黄色。

Sub Lock_by_Color()
Dim colorIndex As Integer
Dim Range As Range

colorIndex = 6
For Each Range In ActiveSheet.UsedRange.Cells
Dim color As Long
 color = Range.Interior.colorIndex
If (color = colorIndex) Then
 Range.Locked = True
Else
 Range.Locked = False
End If
Next Range

ActiveSheet.Protect , DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
ActiveSheet.EnableSelection = xlNoRestrictions
End Sub

相关内容