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

相關內容