简体   繁体   English

如何在打开VBA的情况下解锁工作簿中的空白单元格

[英]How to unlock blank cells in workbook with VBA on open

I have a workbook with multiple sheets that needs all non-blank cells to be locked and protected when opening the workbook. 我有一个包含多个工作表的工作簿,打开工作簿时需要锁定和保护所有非空白单元格。

This will be an employee training record where multiple trainers will be evaluating new staff on skills. 这将是一份员工培训记录,多个培训人员将在此评估新员工的技能。 I would like to prevent anyone from accidentally deleting training data. 我想防止任何人意外删除培训数据。

I found a code that will lock all cells, set all blank cells as unlocked, and protect the individual worksheet, but I'm having trouble applying this across my entire workbook. 我找到了可以锁定所有单元格,将所有空白单元格设置为未锁定并保护单个工作表的代码,但是在将其应用于整个工作簿时遇到了麻烦。

Private Sub Workbook_Open()

    Dim myCell As Range

    Set myCell = Selection
    Cells.Select
    Selection.Locked = True
    myCell.Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.Locked = False

    ActiveSheet.Protect DrawingObjects:=True, _
      Contents:=True, Scenarios:=True
    myCell.Select

End Sub

Ideally, I would also like to add password protection to prevent a new employee from unprotecting the workbook. 理想情况下,我还想添加密码保护,以防止新员工取消对工作簿的保护。 Is this possible within the same code? 是否可以在同一代码中? Can split this to a new question also. 也可以将此分解为一个新问题。

In this case the password for each sheet is secret : 在这种情况下,每个工作表的密码都是秘密的

Sub Tony()
    Dim s As Worksheet

    For Each s In Sheets
        s.Unprotect ("secret")
            s.Cells.Locked = False
            On Error Resume Next
                s.Cells.SpecialCells(xlCellTypeConstants).Locked = True
                s.Cells.SpecialCells(xlCellTypeFormulas).Locked = True
            On Error GoTo 0
        s.Protect ("secret")
    Next s

End Sub

NOTE: 注意:

First make sure this does what you want. 首先确保这符合您的要求。 Only then have your event macro call it. 只有这样,您的事件宏才能调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM