Prevent Users from Performing Certain ActionsAlthough Excel provides overall protection for workbooks and worksheets, this blunt instrument doesn't provide limited privileges to users-unless you do some hacking. You can manage user interactions with your spreadsheets by monitoring and responding to events. Events, as the term suggests, are actions that occur as you work with your workbooks and worksheets. Some of the more common events include opening a workbook, saving it, and closing it when you're through. You can tell Excel to run some Visual Basic code automatically when any one of these events is triggered.
Preventing Save As... in a WorkbookYou can specify that any workbook be saved as read-only by checking the "Read-only recommended" checkbox in the File Sometimes, however, you might want to prevent users from being able to save a copy of your workbook to another directory or folder with or without a different name. In other words, you want users to be able to save on top of the existing file and not save another copy elsewhere. This is particularly handy when more than one person is saving changes to a workbook because you do not end up with a number of different copies of the same workbook, saved with the same name in different folders. The Before Save event you'll be using has existed since Excel 97. As its name suggests, this event occurs just before a workbook is saved, enabling you to catch the user before the fact, issue a warning, and stop Excel from saving.
To insert the code, open your workbook, right-click the Excel icon immediately to the left of the File item on the worksheet menu bar, and select View Code, as shown in Figure 1-6. Figure 1-6. Quick access menu to the private module for the workbook object
Type the following code into the VBE, as shown in Figure 1-7, and then press Alt/ Figure 1-7. Code once it's entered into the private module (ThisWorkbook)
Private Sub workbook_BeforeSave(ByVal SaveAsUI As Boolean,[RETURN]
Cancel As Boolean)
Dim lReply As Long
If SaveAsUI = True Then
lReply = MsgBox("Sorry, you are not allowed to save this" & _
"workbook as another name. Do you wish to save this " & _
"workbook?", vbQuestion + vbOKCancel)
Cancel = (lReply = vbCancel)
If Cancel = False Then Me.Save
Cancel = True
End If
End Sub
Give it a whirl. Select File Preventing Users from Printing a WorkbookPerhaps you want to prevent users from printing your workbook-and probably having it end up in a recycling bin or left on a desk somewhere in plain sight. Using Excel's Before Print event, you can stop them in their tracks. Enter the following code, as before, into the VBE:
Private Sub workbook_BeforePrint(Cancel As Boolean)
Cancel = True
MsgBox "Sorry, you cannot Print from this workbook", vbInformation
End Sub
Press Alt/ If you want to prevent users from printing only particular sheets in your workbook, use this similar code instead:
Private Sub workbook_BeforePrint(Cancel As Boolean)
Select Case ActiveSheet.Name
Case "Sheet1", "Sheet2"
Cancel = True
MsgBox "Sorry, you cannot print this sheet from this workbook",_
vbInformation
End Select
End Sub
Notice you've specified Sheet1 and Sheet2 as the only cases in which printing should be stopped. Of course, these can be the names of any sheets in your workbook; to add more sheets to the list, simply type a comma followed by the sheet name in quotation marks. If you need to prevent the printing of only one sheet, supply just that one name in quotes and drop the comma. Preventing Users from Inserting More WorksheetsExcel lets you protect a workbook's structure so that users cannot delete worksheets, rearrange the order in which they appear, rename them, and so forth. Sometimes, though, you want to prevent just the addition of more worksheets, while still allowing other structural alterations. The following code will get the job done:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Application.DisplayAlerts = False
MsgBox "Sorry, you cannot add any more sheets to this workbook",_ vbInformation
Sh.Delete
Application.DisplayAlerts = True
End Sub
The code first displays the message box with the message and then immediately deletes the newly added sheet when the user clicks OK from the message box. The use of Application.DisplayAlerts = False stops the standard Excel warning that asks users if they really want to delete the sheet. With this in place, users will be unable to add more worksheets to the workbook. Another way to prevent users from adding worksheets is to select Tools |

