【问题标题】:Excel: Lock and protect only filled cells in entire excel reportExcel:仅锁定和保护整个 Excel 报告中的填充单元格
【发布时间】:2013-02-20 16:23:39
【问题描述】:

有没有办法在 Excel 报告的所有工作表中锁定所有填充的单元格(仅)?如果有这方面的 excel 属性,我会很高兴。如果没有 vba 代码就可以了。我找到了 vba 代码,但我们必须提供工作表名称。像这样,我有很多床单,我无法说出所有床单的名称。

VBA 代码:

  Private Sub Workbook_AfterSave(ByVal Success As Boolean)
For Each cl In Sheets("Sheet1").Cells
    If cl = blank Then
        cl.Locked = False
        Else
        cl.Locked = True
    End If
Next
Sheets("Sheet1").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Sheets("Sheet1").EnableSelection = xlUnlockedCells
End Sub

请为此提出更好的方法。

提前致谢。

【问题讨论】:

  • 您可以通过索引而不是名称访问工作表
  • @AndreyGordeev:感谢您的回答。但我想我ll have to mention all sheet index above in my code right??? And what about performance of the action??? Itll 会花很多时间来做这件事。就像它必须在每张纸上增加 20 万个细胞一样,大约有 12 张纸。请为此提出更好的方法。

标签: excel vba


【解决方案1】:

Filled Cells 我假设,单元格有FormulasConstantsComments

查看此示例,您可以将其合并到您的代码中。这不会遍历每个单元格,而是使用SpecialCells

未经测试

Sub Sample()
    Dim Rng As Range
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        .Cells.Locked = False

        On Error Resume Next

        Set Rng = .Cells.SpecialCells(xlCellTypeConstants)
        Rng.Locked = True

        Set Rng = .Cells.SpecialCells(xlCellTypeFormulas)
        Rng.Locked = True

        Set Rng = .Cells.SpecialCells(xlCellTypeComments)
        Rng.Locked = True

        On Error GoTo 0
    End With
End Sub

如果您想将其应用于所有工作表,则只需遍历所有工作表

例如

Sub Sample()
    Dim Rng As Range
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        With ws
            .Cells.Locked = False

            On Error Resume Next

            Set Rng = .Cells.SpecialCells(xlCellTypeConstants)
            Rng.Locked = True

            Set Rng = .Cells.SpecialCells(xlCellTypeFormulas)
            Rng.Locked = True

            Set Rng = .Cells.SpecialCells(xlCellTypeComments)
            Rng.Locked = True

            On Error GoTo 0
        End With
    Next
End Sub

跟进(来自 cmets)

Option Explicit

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim Rng As Range
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        With ws

            .UnProtect

            .Cells.Locked = False

            On Error Resume Next

            Set Rng = .Cells.SpecialCells(xlCellTypeConstants)
            Rng.Locked = True

            Set Rng = .Cells.SpecialCells(xlCellTypeFormulas)
            Rng.Locked = True

            Set Rng = .Cells.SpecialCells(xlCellTypeComments)
            Rng.Locked = True

            On Error GoTo 0

           .Protect
           .EnableSelection = xlUnlockedCells
        End With
    Next
End Sub

【讨论】:

  • 悉达多:感谢您的回答。我m actually a big fan of you. The above vba code will work for filled cells??? Im 实际上在这里没有得到你的逻辑。请更具体地解释。我应该什么时候调用这个函数??
  • 是的,它适用于填充单元格。我可以解释 SpecialCells 的作用,但如果您查看 Excel 帮助,SpecialCells 已经很好地涵盖了。
  • Siddharth : 谢谢,好的,我会了解 SpecialCells,但是这个函数应该在 'aftersave' 事件中调用,对吧?
  • 不,必须在Workbook_BeforeSave 中使用。让我更新上面的代码。稍后查看跟进
  • 已更新 :) 如果您遇到任何问题,请告诉我,因为我没有测试过
猜你喜欢
  • 2019-03-28
  • 1970-01-01
  • 2017-01-23
  • 1970-01-01
  • 2019-07-21
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
  • 2017-02-12
相关资源
最近更新 更多