Having cell show if worksheet is protected or unprotected

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
821
Office Version
  1. 365
Platform
  1. Windows
As im sure you can tell in the code below that when I select cell E1 in sheet 2 it calls to run a macro to toggle protect and unprotect of sheet 2. I was wandering if there could be something added to the LockUnlock() code so cell E1 would actually show the text "PROTECTED" OR UNPROTECTED" depending on the worksheets status.


VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    If Target.Address = "$E$1" Then
        Call LockUnlock
    End If
End Sub

Code:
Sub LockUnlock()
If ActiveSheet.ProtectContents = True Then
ActiveSheet.Unprotect ""
Else
ActiveSheet.Protect ""
End If
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Should be pretty straightforward:
Rich (BB code):
Sub LockUnlock()
    If ActiveSheet.ProtectContents = True Then
        ActiveSheet.Unprotect ""
        Range("E1").Value = "UNPROTECTED"
    Else
        Range("E1").Value = "PROTECTED"
        ActiveSheet.Protect ""
    End If
End Sub
 
Upvote 0
Solution
Thanks Joe worked like a charm. Could there be something added to make the cell blue and font white when protected and cell green and font black when Unprotected?
 
Upvote 0
Thanks Joe worked like a charm. Could there be something added to make the cell blue and font white when protected and cell green and font black when Unprotected?
Sure. But I will let you do that part yourself. It will be a great learning/teaching exercise.

For real simple snippets of code (like changing colors, etc), simply turn on your Macro Recorder and record yourself manually making the changes you want to see.
Then stop the Macro Recorder and view the code you just recorded. It will contain the commands you need to put in your code. So just copy and insert those parts over into your code.
The Macro Recorder is a great tool for this sort of thing.

One caveat: The Macro Recorder is very literal, and records every cell selection. So if you were recording yourself setting cell A4 to "Dog", you would have a section of code that looks something like this:
VBA Code:
    Range("A4").Select
    ActiveCell.FormulaR1C1 = "Dog"
In VBA code, it usually isn't necessary to select a range first in order to work with it. So whenever you have one line of code that sends in "Select" and the next begins with "Selection" or "ActiveCell", you can usually combine them together like this:
VBA Code:
    Range("A4").FormulaR1C1 = "Dog"
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top