WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
To change the protection status of a workbook using a Visual Basic
subroutine contained in that workbook, use any of the following methods.
Method 1
Use the SendKeys statement to protect or unprotect the workbook. Note that
this method works with Microsoft Excel for Windows (you cannot send
keystrokes to applications running under the Macintosh operating system).
The following example uses the SendKeys statement to send the key strokes
necessary to protect the workbook. The OnTime command is used to resume
another macro one second later. Note that Application.ScreenUpdating will
not affect this procedure because the macro is exited and then the key
strokes are executed from the keyboard buffer.
You should enter the following code as the last piece of code in the
module.
' You can skip this line if you are adding this code to the
' end of an existing macro
Sub ProtectWorkbook()
' This will return to a macro named My_Macro after protecting
' the workbook. This line is not necessary if you do not need
' to resume after the workbook is protected
Application.OnTime Now + TimeValue("00:00:01"), "My_Macro"
' Note that you can use %(tpp) instead of %(tpw) to
' protect only the active sheet.
Application.SendKeys "%(tpw){ENTER}"
' The procedure must end before the keys will be sent
End Sub
Method 2
Unhide the module that contains the subroutine before using the Protect
method. Note that if you use this method, you cannot protect the structure
of the workbook, and you cannot use the Unprotect method (because if the
structure of the workbook is currently protected, you cannot hide or unhide
a sheet in the workbook).
To use a subroutine contained in a hidden module ("ModuleName") to protect
the workbook (not the structure) in which it is contained, you could use
the following code:
' Unhide the module that contains this subroutine.
ThisWorkbook.Modules("ModuleName").Visible = True
' Protect the workbook by using the Protect method.
ThisWorkbook.Protect Structure:=False, Windows:=True
' Re-hide the module that contains this subroutine.
ThisWorkbook.Modules("ModuleName").Visible = False
Method 3
Set the Visible property of the Module containing the Protect and Unprotect
macros to xlVeryHidden rather than False. The Structure and Windows
arguments are optional.
For example:
ThisWorkbook.Modules("ModuleName").Visible = xlVeryHidden
REFERENCES
For more information about the Protect Method or the Unprotect Method,
choose the Search button in MS Excel Visual Basic Help (Macintosh) or the
Visual Basic Reference (Windows), and type the following:
protection: workbook structure