Password protect excel sheet

Saturday, July 26, 2008

Go to excel, and in any sheet right click in the little Excel icon file and choose "View code".

That should take you to the VB Editor.

Delete the text that appears (If you had nothing there)

Private Sub Workbook_Open()

End Sub

Now, copy this text:


Public PvSh As String
Public Pwd As String

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Pwd = "" Then
If Sh.Name = "Sheet2" Then
Num = ActiveWindow.Index
Windows(Num).Visible = False
If Application.InputBox("Enter Password", "Password") <> "airplane" Then
MsgBox "Incorrect Password", vbCritical, "Error"
Application.EnableEvents = False
Sheets(PvSh).Select
Application.EnableEvents = True
Else
Pwd = "airplane"
End If
Windows(Num).Visible = True
End If
End If
End Sub


http://www.mrexcel.com/archive/VBA/1646.html

Posted by Pithaly at 12:54 PM  

4 comments:

I've actually used this for a submission of a confidential file. Place the code in sheet 1. Sheet 2 will be password protected.

Pithaly said...
August 2, 2008 at 1:47 AM  

wow! cool .. i really wanted somethin like tht because of the fact that i'm working like shit on something at work and then i suspect my work's going to be passed off as his by my colleague .. so wanted to do somethin abt it.
thnxx!

Anonymous said...
August 3, 2008 at 12:24 AM  

Anonymous,

One thing:

When you save the file, make sure that you are in tab 1 which has the code, and not in the tab (2) which has the data, else the file will save as opened. (Btw, the data can be in any tab. just make sure the code in in another tab).

I like to mention in tab 1 that the data is in tab 2, password protected, lest the user thinks he's been sent a blank file lol!

This is especially important if you want to transfer the file to someone else. On opening, it will open in tab 1, and on the user clicking tab 2, a password would be asked for.

Pithaly said...
August 3, 2008 at 5:39 PM  

oh cool thanx :)

Anonymous said...
August 3, 2008 at 5:45 PM  

Post a Comment