将Excel工作表的保护状态传递给单元格

我很好奇是否可以将Excel工作表的保护状态传递给该工作表的单元格。 例如

  • 工作表1被locking编辑…单元格A1将被编程为说“locking”
  • Sheet1被解锁…单元格A1会说“解锁”

工作表上的button将用于打开和closures工作表保护。

使用workbook_open事件打开时,我的工作表将被locking。

这是一个表格,我不希望公式在使用时被全部使用,但是可能需要完全访问。 它更多地提醒用户他们处于“解锁”模式,所以要格外小心。

使用VBA已成定局?

我是VBA noob,但不介意使用代码作为解决scheme

任何想法或build议欢迎

您可以使用Sheet1上的ActiveXbutton中的代码来简单地执行此操作

 Const strPAss = "test" Private Sub CommandButton1_Click() If ActiveSheet.ProtectContents Then ActiveSheet.Unprotect strPAss [a1].Value = "unlocked" Else [a1].Value = "locked" ActiveSheet.Protect strPAss End If End Sub 

把它放在工作表的代码模块中,这会在状态栏中提示提示(这样可以避免为了将状态写入单元格A1而需要locking/解锁表单)。

把它放在Sheet1代码模块中。 每次激活sheet1时macros都会执行。

 Private Sub Worksheet_Activate() If ActiveSheet.ProtectContents then Application.StatusBar = "This sheet is protected" Else: Application.StatusBar = "This sheet is unprotected" End If End Sub Private Sub Worksheet_Deactivate() Application.StatusBar = False End Sub 

要保护/取消保护工作表,您可以将其添加到“插入”>“模块”中。 然后将这些macros附加到单独的命令button,或者从Developer> Macrosfunction区运行。

 Const myPassword as String = "password" '<-- replace "password" with your password Sub Sht1Protect() Sheet1.Protect myPassword End Sub Sub Sht1Unprotect() Sheet1.Unprotect myPassword End Sub 

要确保在closures文件时纸张始终受到保护,请将其插入到工作簿代码模块中

 Private Sub Workbook_Close() Sht1Protect End Sub 

您可能需要额外的处理来控制文件是否保存/未保存等