|
|
|
|
|
|
Excel
|
Protect cell or cells on sheet without protecting sheet
|
|
Ease of Use
|
Easy
|
Version tested with
|
2003
|
Submitted by:
|
Simon Lloyd
|
Description:
|
This enables the user to protect a single cell or group of cells without protecting the whole sheet using a userform for password entry to allow access to the cell
|
Discussion:
|
If you have difficulties in protecting the whole sheet for some reason or would like to protect something in a shared workbook but allow access on entry of a password. This could be for a single cell or cell range that only requires certain people to edit the Data in them. Using this would give you the flexibility of being able to protect or unprotect in shared workbook
|
Code:
|
instructions for use
|
Private Sub Worksheet_Activate()
Set OriginalCell = ActiveCell
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
Application.ScreenUpdating = False
Set ProtectedCell = Target
UserForm1.Show
Application.ScreenUpdating = True
Else
Set OriginalCell = Target
End If
End Sub
Private Sub CmndSubmit_Click()
If UserForm1.TextBox1 <> "Password" Then
MsgBox "Incorrect Password", vbOKOnly, "Warning"
OriginalCell.Select
Unload Me
Else
ProtectedCell.Select
Unload Me
Application.EnableEvents = True
End If
End Sub
Private Sub CmndCancel_Click()
MsgBox "Cell Access Denied", vbExclamation, "Access Status"
OriginalCell.Select
Unload Me
End Sub
Private Sub UserForm_initialize()
UserForm1.Caption = UserForm1.Caption & ProtectedCell.Address
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
Cancel = True
Call CmndCancel_Click
End If
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Sh As Worksheet
Dim Ctr As Integer
Ctr = 0
For Each Sh In Sheets
If Sh.Visible Then
Ctr = Ctr + 1
End If
Next Sh
If Ctr = 1 Then
MsgBox "You can not hide the only sheet in the workbook, you must have at least 2 sheets visible before closing", vbOKOnly, "Insufficient Sheets!"
Cancel = True
Exit Sub
End If
Sheets("Sheet1").Visible = xlVeryHidden
ThisWorkbook.Save
End Sub
Private Sub Workbook_Open()
Sheets("Sheet1").Visible = True
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI = True Then Cancel = True
End Sub
Option Explicit
Public OriginalCell As Range
Public ProtectedCell As Range
|
How to use:
|
- Firstly we need to open the VBE (visual basic editor) window, this is done by pressing Alt+F11 together, once the window opens you will notice a gray window pane to the left and a VBA Project window to your top left displaying any worksheets and modules, below this you will see the properties window for the current object you are viewing. We now need to create a userform, this is done by right clicking in the VBA project window and choose INSERT | USERFORM, once you have done this the userform object will appear as a gray box with a blue border, to the left of this a small panel will appear with the available controls for the userform this is called the CONTROL TOOLBOX.
- Select the Command Button icon by clicking on it (a single gray oblong button), move your mouse pointer to the userform you will see a "+" with a small gray box attached to the bottom left, simply hold down your left mouse button and "Draw" the button to whatever size you want, now repeat the process for another command button and for a Textbox!
- Once you have created these click any one of them once, notice the window to the bottom left of your screen change, this is the property window for that item, click the command button once and in the properties window find the title that says Caption and change the word CommandButton1 to whatever you would like to be displayed on the button,repeat for the second button but call it "cancel", also in the properties window just above "Caption" you will see the title "Cancel" change this to True to prevent the user from using the Escape button to get out of the userform and ending the macro prematurely, now click the textbox once find the title in the properties window that says PasswordChar next to this type a single character that you would like to be displayed when text is typed in to this box later (i chose the standard "*")
- Now we will get to work pasting the items of code above in to their respective places, in the VBA Project window to the left you will see a userform icon right click it once and choose "View Code", now copy and paste the items from the code above that are labeled for pasting into the userform.
- Having finished with the userform for now lets paste the other items of code, right click on the ThisWorkbook icon choose "View Code" and paste the items of code labeled as such from the code above in to this module, next perform the same action on Sheet 1 code module and paste the items of code labeled for pasting to it from the above in to that, your final task is to right click in the VBA Project window and choose INSERT | MODULE and paste the last item of code in to this as this sets Global Variables for use in our userform and worksheet module
-
- You are now ready to test the code!
|
Test the code:
|
- In your workbook select sheet 1 and try to access cell A1, you should get a userform appear waiting for your password, type any thing you like, the characters should appear as * click your command button, assuming you typed the password incorrect you will be presented with a message box telling you its incorrect and then you will be transported back to the last cell you were at, now click A1 again and type "Password" (it is case sensitive!) hit the command button and you should find that you now stay at cell A1 and can type in it!
-
- Now hide all the worksheets in your workbook except for sheet1, now try to close the workbook by either clicking the "X" or choosing File and then Close, you should be presented with a message box telling you you cannot do that, unhide another sheet or sheets try the same, you should find that the workbook will close and save no problem. Now open the workbook and try to save the workbook as a different name, you should find that the Save As dialog box does not appear. Close the workbook for one last test, open another workbook set your macro security to high and close the workbook, try opening the workbook we have been working on but when resented with the Macros security warning and being asked to enable macro's choose No you should find that sheet 1 is not visible nor available from the toolbar under FORMAT | SHEET | UNHIDE. With this workbook open reset you macro security to the desired level and close, you may then reopen the workbook and continue using it normally provided you have agreed to run macros.
-
- If you want to change the cell or cells that are protected then access the worksheet module and change the named cell in the (Target,Range("Xn") where Xn is A1.
|
Sample File:
|
Cell Password No Save As.zip 13.08KB
|
Approved by mdmackillop
|
This entry has been viewed 699 times.
|
|