Question

I'm curious as to whether it's possible to pass the protection status of an excel worksheet to a cell of that worksheet. e.g.

  • Sheet1 is locked for editing...cell A1 would be programmed to say "locked"
  • Sheet1 is unlocked...cell A1 would say "unlocked".

A button on the sheet would be used to toggle worksheet protection on and off.

My sheet will be locked upon opening using a workbook_open event.

This is for a sheet where I don't want the formulae getting all mucked up upon use, but where full access might be required. Its more as a reminder to the user that they are in "Unlocked" Mode so to be extra careful.

Is using VBA a foregone conclusion?

I'm a VBA noob but don't mind using code as a solution for this

Any thoughts or suggestions welcome

Was it helpful?

Solution

You could use code in an ActiveX button on Sheet1 to do this simply

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

OTHER TIPS

Put this in the worksheet's code module, which will place a reminder in the Status Bar (this avoids needing to lock/unlock the sheet in order to write the status in to cell A1).

Put this in Sheet1 code module. The macro will execute every time sheet1 is activated.

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

To protect/unprotect the worksheet you could add this to an Insert>Module. Then attach these macros to separate command buttons, or run from the Developer>Macros ribbon.

Const myPassword as String = "password" '<-- replace "password" with your password

Sub Sht1Protect()
    Sheet1.Protect myPassword
End Sub

Sub Sht1Unprotect()
    Sheet1.Unprotect myPassword
End Sub

To ensure the sheet is always protected when you close the file, insert this in the Workbook code module

Private Sub Workbook_Close()
    Sht1Protect
End Sub

You may need additional handling to control whether the file is saved/not saved etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top