我有这个 excel 表,我想在其中保护某些单元格免于格式化和编辑。所有这些单元格都用特定颜色着色。
该工作表非常大,因此我正在寻找一种方法来锁定所有这些单元格,然后能够在不更改我想要锁定的单元格的情况下批量格式化所有其他单元格。
有没有办法告诉excel锁定具有特定颜色的单元格?
是的,使用 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
以上假设您没有合并的单元格并且您的工作表不受保护。
如果您不确定所需的 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)
| 归档时间: |
|
| 查看次数: |
12024 次 |
| 最近记录: |