Different password for different worksheet

F

Freshman

Dear expert,

I want to create a workbook with different worksheets for different users.
For security purpose, I want different worksheet has a different password to
protect so that the only authorized user can edit the content on his/her
designated worksheet.
For example, sheet "marketing" has a password "1234" and sheet "sales" has a
password "1235" to protect. Is it possible? Please kindly advise.

Thanks in advance.
 
M

macropod

Hi Freshman,

In short, yes, It's possible. For any given sheet look under tools|Protection|Protect Sheet. The problem you're likely to have,
however, is that your users will forget to re-protect the worksheet before saving & closing the workbook.

For something more sophisticated, you could write a macro that prevents a user accessing a worksheet unless they provide a password
for it.
 
S

Stefi

Set Tools>Protection>Sheet protection individually for each sheets with
different passwords!

Regards,
Stefi

„Freshman†ezt írta:
 
F

Freshman

Hi macropod,

I'm interested to have a macro to protect worksheets with different
passwords, then I can assign the passwords to each individual user. May I
have the code as I'm a VBA idiot.

Thanks.

macropod said:
Hi Freshman,

In short, yes, It's possible. For any given sheet look under tools|Protection|Protect Sheet. The problem you're likely to have,
however, is that your users will forget to re-protect the worksheet before saving & closing the workbook.

For something more sophisticated, you could write a macro that prevents a user accessing a worksheet unless they provide a password
for it.

--
Cheers
macropod
[MVP - Microsoft Word]


Freshman said:
Dear expert,

I want to create a workbook with different worksheets for different users.
For security purpose, I want different worksheet has a different password to
protect so that the only authorized user can edit the content on his/her
designated worksheet.
For example, sheet "marketing" has a password "1234" and sheet "sales" has a
password "1235" to protect. Is it possible? Please kindly advise.

Thanks in advance.
 
F

Freshman

Thanks Stefi.

Stefi said:
Set Tools>Protection>Sheet protection individually for each sheets with
different passwords!

Regards,
Stefi

„Freshman†ezt írta:
 
M

macropod

Hi Freshman,

Here's some code to get you started. It goes into your workbook's 'ThisWorkbook' vba module.

Option Explicit
Dim User As String
Dim UPwd As String
Dim WPwd As String
Dim Err As Boolean
Dim wsSheet As Worksheet
Dim wsActvSht As Worksheet

Private Sub Workbook_Open()
WPwd = "" 'Inset the Workbook Password between the double quotes
Set wsActvSht = ThisWorkbook.Sheets("Sheet1") ' A worksheet that must remain visible
If ThisWorkbook.ProtectStructure = True Or ThisWorkbook.ProtectWindows = True Then ThisWorkbook.Unprotect WPwd
wsActvSht.Activate
For Each wsSheet In ActiveWorkbook.Worksheets
With wsSheet
If .Name = wsActvSht.Name Then
.Visible = xlSheetVisible
Else
.Visible = xlSheetVeryHidden
End If
End With
Next wsSheet
Restart:
User = InputBox("Please Input your Workbook Username")
UPwd = InputBox("Please Input your Workbook Password")
For Each wsSheet In ActiveWorkbook.Worksheets
With wsSheet
If .Name = User Then
On Error GoTo Restart
If .ProtectContents = True Then .Unprotect UPwd
.Visible = xlSheetVisible
.Activate
Exit Sub
End If
End With
Next wsSheet
ThisWorkbook.Protect Password:=WPwd, Structure:=True, Windows:=True
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.ProtectStructure = True Or ThisWorkbook.ProtectWindows = True Then ThisWorkbook.Unprotect WPwd
For Each wsSheet In ActiveWorkbook.Worksheets
With wsSheet
If .Name = User Then .Protect UPwd
If .Name <> wsActvSht.Name Then .Visible = xlSheetVeryHidden
End With
Next wsSheet
wsActvSht.Activate
ThisWorkbook.Protect Password:=WPwd, Structure:=True, Windows:=True
End Sub

The code works by automatically hiding all except a 'main' worksheet upon opening and closing. It then asks the user for their
worksheet name and password. If the correct password for the named sheet is given, it is displayed and unprotected; otherwise the
prompt is repeated. If the prompts are left empty, the code does nothing more and exits.

The code is also written around the principle that the workbook itself should be protected - see the relevant comments in the code.
This is to protect its structure etc, so that you won't run into problems with sheets being renamed etc.

You'll probably want to add more code to trap 'Save' events, in case the user saves the workbook with their worksheet visible. I'll
leave that to you.

--
Cheers
macropod
[MVP - Microsoft Word]


Freshman said:
Hi macropod,

I'm interested to have a macro to protect worksheets with different
passwords, then I can assign the passwords to each individual user. May I
have the code as I'm a VBA idiot.

Thanks.

macropod said:
Hi Freshman,

In short, yes, It's possible. For any given sheet look under tools|Protection|Protect Sheet. The problem you're likely to have,
however, is that your users will forget to re-protect the worksheet before saving & closing the workbook.

For something more sophisticated, you could write a macro that prevents a user accessing a worksheet unless they provide a
password
for it.

--
Cheers
macropod
[MVP - Microsoft Word]


Freshman said:
Dear expert,

I want to create a workbook with different worksheets for different users.
For security purpose, I want different worksheet has a different password to
protect so that the only authorized user can edit the content on his/her
designated worksheet.
For example, sheet "marketing" has a password "1234" and sheet "sales" has a
password "1235" to protect. Is it possible? Please kindly advise.

Thanks in advance.
 
R

ryguy7272

This is how I do it...

Place the following code in a Sheet (not a module)
Basically, right-click the sheet and click ‘View Code’
Private Sub CommandButton1_Click()


Dim i_pwd As String

i_pwd = InputBox("Please Enter Password to Unhide Sheet", "Unhide Sheet...")
If i_pwd = "" Then
Exit Sub
End If

'#1
Select Case (i_pwd)
Case Is = "showmktg"
Worksheets("mktg1").Visible = True
Worksheets("mktg2").Visible = True
Worksheets("mktg2").Visible = True
Sheets("mktg1").Select

'#2

Case Is = "showsales"
Worksheets("sales1").Visible = True
Worksheets("sales2").Visible = True
Worksheets("sales3").Visible = True
Sheets("sales1").Select



'#3
Case Is = "showall"
Call ShowSheets


Case Else
MsgBox "Incorrect password; no action taken.", vbInformation, _
"Unhide Sheet..."
End Select

Exit Sub


End Sub


Sub ShowSheets()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
If (sh.Name) <> "Password" Then
If sh.Visible = xlSheetVeryHidden Then
sh.Visible = True
End If
End If
Next sh

End Sub


Place following code in sheet called ‘ThisWorkbook’
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call HideSheets
End Sub


Regards,
Ryan---
 
A

amu tak

hi.
the first code by macropod is awesome..
what changes will i have to be made if i want the multiple worksheet to remain visible untill i close my workbook.

For ex.
i opened one worksheet after entering password
but i want to open another worksheet by entering different password .
now i want them to remain visible till i close the workbook

amutak



macropod wrote:

Hi Freshman,Here's some code to get you started.
28-Jul-08

Hi Freshman

Here's some code to get you started. It goes into your workbook's 'ThisWorkbook' vba module

Option Explici
Dim User As Strin
Dim UPwd As Strin
Dim WPwd As Strin
Dim Err As Boolea
Dim wsSheet As Workshee
Dim wsActvSht As Workshee

Private Sub Workbook_Open(
WPwd = "" 'Inset the Workbook Password between the double quote
Set wsActvSht = ThisWorkbook.Sheets("Sheet1") ' A worksheet that must remain visibl
If ThisWorkbook.ProtectStructure = True Or ThisWorkbook.ProtectWindows = True Then ThisWorkbook.Unprotect WPw
wsActvSht.Activat
For Each wsSheet In ActiveWorkbook.Worksheet
With wsShee
If .Name = wsActvSht.Name The
.Visible = xlSheetVisibl
Els
.Visible = xlSheetVeryHidde
End I
End Wit
Next wsShee
Restart
User = InputBox("Please Input your Workbook Username"
UPwd = InputBox("Please Input your Workbook Password"
For Each wsSheet In ActiveWorkbook.Worksheet
With wsShee
If .Name = User The
On Error GoTo Restar
If .ProtectContents = True Then .Unprotect UPw
.Visible = xlSheetVisibl
.Activat
Exit Su
End I
End Wit
Next wsShee
ThisWorkbook.Protect Password:=WPwd, Structure:=True, Windows:=Tru
End Su

Private Sub Workbook_BeforeClose(Cancel As Boolean
If ThisWorkbook.ProtectStructure = True Or ThisWorkbook.ProtectWindows = True Then ThisWorkbook.Unprotect WPw
For Each wsSheet In ActiveWorkbook.Worksheet
With wsShee
If .Name = User Then .Protect UPw
If .Name <> wsActvSht.Name Then .Visible = xlSheetVeryHidde
End Wit
Next wsShee
wsActvSht.Activat
ThisWorkbook.Protect Password:=WPwd, Structure:=True, Windows:=Tru
End Su

The code works by automatically hiding all except a 'main' worksheet upon opening and closing. It then asks the user for their
worksheet name and password. If the correct password for the named sheet is given, it is displayed and unprotected; otherwise the
prompt is repeated. If the prompts are left empty, the code does nothing more and exits

The code is also written around the principle that the workbook itself should be protected - see the relevant comments in the code.
This is to protect its structure etc, so that you won't run into problems with sheets being renamed etc

You'll probably want to add more code to trap 'Save' events, in case the user saves the workbook with their worksheet visible. I'll
leave that to you

--
Cheer
macropo
[MVP - Microsoft Word


Previous Posts In This Thread:

Different password for different worksheet
Dear expert

I want to create a workbook with different worksheets for different users.
For security purpose, I want different worksheet has a different password to
protect so that the only authorized user can edit the content on his/her
designated worksheet
For example, sheet "marketing" has a password "1234" and sheet "sales" has a
password "1235" to protect. Is it possible? Please kindly advise

Thanks in advance.

Hi Freshman,In short, yes, It's possible.
Hi Freshman

In short, yes, It's possible. For any given sheet look under tools|Protection|Protect Sheet. The problem you're likely to have,
however, is that your users will forget to re-protect the worksheet before saving & closing the workbook

For something more sophisticated, you could write a macro that prevents a user accessing a worksheet unless they provide a password
for it

--
Cheer
macropod
[MVP - Microsoft Word]



Set Tools>Protection>Sheet protection individually for each sheets with
Set Tools>Protection>Sheet protection individually for each sheets with
different passwords!

Regards,
Stefi

???Freshman??? ezt ??rta:

Hi macropod,I'm interested to have a macro to protect worksheets with
Hi macropod,

I'm interested to have a macro to protect worksheets with different
passwords, then I can assign the passwords to each individual user. May I
have the code as I'm a VBA idiot.

Thanks.

:

RE: Different password for different worksheet
Thanks Stefi.

:

Hi Freshman,Here's some code to get you started.
Hi Freshman,

Here's some code to get you started. It goes into your workbook's 'ThisWorkbook' vba module.

Option Explicit
Dim User As String
Dim UPwd As String
Dim WPwd As String
Dim Err As Boolean
Dim wsSheet As Worksheet
Dim wsActvSht As Worksheet

Private Sub Workbook_Open()
WPwd = "" 'Inset the Workbook Password between the double quotes
Set wsActvSht = ThisWorkbook.Sheets("Sheet1") ' A worksheet that must remain visible
If ThisWorkbook.ProtectStructure = True Or ThisWorkbook.ProtectWindows = True Then ThisWorkbook.Unprotect WPwd
wsActvSht.Activate
For Each wsSheet In ActiveWorkbook.Worksheets
With wsSheet
If .Name = wsActvSht.Name Then
.Visible = xlSheetVisible
Else
.Visible = xlSheetVeryHidden
End If
End With
Next wsSheet
Restart:
User = InputBox("Please Input your Workbook Username")
UPwd = InputBox("Please Input your Workbook Password")
For Each wsSheet In ActiveWorkbook.Worksheets
With wsSheet
If .Name = User Then
On Error GoTo Restart
If .ProtectContents = True Then .Unprotect UPwd
.Visible = xlSheetVisible
.Activate
Exit Sub
End If
End With
Next wsSheet
ThisWorkbook.Protect Password:=WPwd, Structure:=True, Windows:=True
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.ProtectStructure = True Or ThisWorkbook.ProtectWindows = True Then ThisWorkbook.Unprotect WPwd
For Each wsSheet In ActiveWorkbook.Worksheets
With wsSheet
If .Name = User Then .Protect UPwd
If .Name <> wsActvSht.Name Then .Visible = xlSheetVeryHidden
End With
Next wsSheet
wsActvSht.Activate
ThisWorkbook.Protect Password:=WPwd, Structure:=True, Windows:=True
End Sub

The code works by automatically hiding all except a 'main' worksheet upon opening and closing. It then asks the user for their
worksheet name and password. If the correct password for the named sheet is given, it is displayed and unprotected; otherwise the
prompt is repeated. If the prompts are left empty, the code does nothing more and exits.

The code is also written around the principle that the workbook itself should be protected - see the relevant comments in the code.
This is to protect its structure etc, so that you won't run into problems with sheets being renamed etc.

You'll probably want to add more code to trap 'Save' events, in case the user saves the workbook with their worksheet visible. I'll
leave that to you.

--
Cheers
macropod
[MVP - Microsoft Word]



This is how I do it...
This is how I do it...

Place the following code in a Sheet (not a module)
Basically, right-click the sheet and click ???View Code???
Private Sub CommandButton1_Click()


Dim i_pwd As String

i_pwd = InputBox("Please Enter Password to Unhide Sheet", "Unhide Sheet...")
If i_pwd = "" Then
Exit Sub
End If

'#1
Select Case (i_pwd)
Case Is = "showmktg"
Worksheets("mktg1").Visible = True
Worksheets("mktg2").Visible = True
Worksheets("mktg2").Visible = True
Sheets("mktg1").Select

'#2

Case Is = "showsales"
Worksheets("sales1").Visible = True
Worksheets("sales2").Visible = True
Worksheets("sales3").Visible = True
Sheets("sales1").Select



'#3
Case Is = "showall"
Call ShowSheets


Case Else
MsgBox "Incorrect password; no action taken.", vbInformation, _
"Unhide Sheet..."
End Select

Exit Sub


End Sub


Sub ShowSheets()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
If (sh.Name) <> "Password" Then
If sh.Visible = xlSheetVeryHidden Then
sh.Visible = True
End If
End If
Next sh

End Sub


Place following code in sheet called ???ThisWorkbook???
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call HideSheets
End Sub


Regards,
Ryan---

--
RyGuy


:

EggHeadCafe - Software Developer Portal of Choice
SharePoint Site Provisioning ? The Real World
http://www.eggheadcafe.com/tutorial...7-9842af7b5854/sharepoint-site-provision.aspx
 
M

Minu

Dear expert,

I am using MS Excel 2007. I want to create a workbook with different
worksheets for different users to be able to view and edit the content on
his/her designated worksheet. i.e. For security purpose, I want different
worksheet has a different password to
protect so that the only authorized user can view & edit the content on
his/her
designated worksheet.
For example, sheet "marketing" has a password "1234" and sheet "sales" has a
password "1235" to view and protec. Is it possible? Please kindly advise.

Thanks in advance.
 
M

Minu

Thank you Don. But how to set individual excel sheet passwords for viewing
and editing. i.e only desginated user to view and edit. Appreciate some
help in this matter.
 
G

Gord Dibben

Keeping in mind that internal security is very weak in Excel.

Requires VBA code and some passwords or login names.

Sample code.......................

Note: the following is contingent upon users enabling macros.

If they don't only the "Dummy" sheet will be visible with a large message
stating "By disabling macros you have rendered this workbook unusuable.
Please close and re-open with macros enabled"

I assume you are on a network(LAN) with users logging into the system.

I would set it up so that whichever user's login name is flagged, all sheets
except that user would be hidden.

No password to open the workbook or sheet protection, just code to make a
user's sheet visible.

In the Thisworkbook Module....................

Private Sub Workbook_Open()
Dim pword As String
On Error GoTo endit
Select Case Environ("Username")

'if a login is not used change to
'pword = InputBox("Enter Your Password")
'Select Case pword

Case Is = "Gord": Sheets("Gordsheet").Visible = True
Case Is = "Pete": Sheets("Petesheet").Visible = True
End Select
Sheets("Dummy").Visible = False
Exit Sub
endit:
MsgBox "Incorrect Password"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sht As Worksheet
Application.ScreenUpdating = False
Sheets("Dummy").Visible = xlSheetVisible
For Each sht In ActiveWorkbook.Sheets
If sht.Name <> "Dummy" Then
sht.Visible = xlSheetVeryHidden
End If
Next sht
Application.ScreenUpdating = True
ThisWorkbook.Save
End Sub

To allow you to see all sheets and edit them.

In a general module...............

Sub UnHideAllSheets()
Application.ScreenUpdating = False
Dim n As Single
For n = 1 To Sheets.Count
Sheets(n).Visible = True
Next n
Application.ScreenUpdating = True
End Sub

Naturally you want all this code invisible to the users.

Right-click on the workbook/project in VBE and select VBAProject Properties
and "Lock project for viewing"

Enter a password.


Gord Dibben MS Excel MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top