|
|
|
|
|
|
Excel
|
Check if Worksheet is protected
|
|
Ease of Use
|
Easy
|
Version tested with
|
97, 2003
|
Submitted by:
|
Ken Puls
|
Description:
|
This function checks to see if a worksheet is protected. Returns True if so or False if not
|
Discussion:
|
This routine can be used to test if a worksheet is protected before attempting to do something to/with it through VBA code. It can be called through VBA, but unfortunately cannot be entered directly in a worksheet cell. Only the SheetProtected function (below) is required to test the protection setting, the "SimpleTest" subroutine is for illustration purposes only.
|
Code:
|
instructions for use
|
Option Explicit
Private Function SheetProtected(TargetSheet As Worksheet) As Boolean
If TargetSheet.ProtectContents = True Then
SheetProtected = True
Else
SheetProtected = False
End If
End Function
Sub SimpleTest()
Dim ws As Worksheet
Set ws = ActiveSheet
If SheetProtected(ws) Then
MsgBox "Sorry, but " & ws.Name & " is protected!", _
vbOKOnly + vbInformation, ws.Name & " is protected!"
Else
MsgBox "Hooray! " & ws.Name & " is not protected!", _
vbOKOnly + vbInformation, ws.Name & " is unprotected!"
End If
End Sub
|
How to use:
|
- Copy above code.
- In Excel press Alt + F11 to enter the VBE.
- Press Ctrl + R to show the Project Explorer.
- Right-click desired file on left (in bold).
- Choose Insert -> Module.
- Paste code into the right pane.
- Press Alt + Q to close the VBE.
- Save workbook before any other changes.
|
Test the code:
|
- In Excel press Alt + F8 to call the macro
- Choose SimpleTest from the dropdown list
- A messagebox will pop up to display the current protection setting
- Clear the messagebox
- From the Tools Menu, choose Protection/(un)protect sheet (assign/enter a password if necessary)
- Follow steps 1 & 2 above to check sheet protection again
|
Sample File:
|
SheetProtectionFunction.zip 10.94KB
|
Approved by mdmackillop
|
This entry has been viewed 303 times.
|
|