ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Protecting Sheet (https://www.excelbanter.com/excel-programming/369473-protecting-sheet.html)

Nad

Protecting Sheet
 
Hi
I used this code to protect my sheet
Private Sub Worksheet_Activate()
psw = InputBox("Please insert password to view data.", "Password Checker")
If psw = "EPM" Then
Sheets("ABD").Select
Else
MsgBox "Incorrect Password"
End If
End Sub
But, even if i insert the wrong password my sheet become visible/active.
Pls tell me where i am wrong.
Thanks


somethinglikeant

Protecting Sheet
 
Hi Nad,

You could probably do this without using macros, but here is a nice
little solution hopefully:-

First Protect the sheet by going to Tools Protection Protect Sheet
set the password as the one you stated in your code EPM
Click OK

Then write this code into the Activate Worksheet module for this
sheet:-

:=======================

Private Sub Worksheet_Activate()

If ActiveSheet.ProtectContents = True Then
psw = InputBox("Please insert password to view data.", "Password
Checker")
If psw = "EPM" Then
Sheets("ABD").Unprotect ("EPM")
Else
MsgBox "Incorrect Password"
End If
End If

End Sub

:========================

Should work OK, let me know if there aare any problems

somethinglikeant
http://www.excel-ant.co.uk


Nad

Protecting Sheet
 
Sorry, its not working. i got the same result as before.
Regards,
Nad

"somethinglikeant" wrote:

Hi Nad,

You could probably do this without using macros, but here is a nice
little solution hopefully:-

First Protect the sheet by going to Tools Protection Protect Sheet
set the password as the one you stated in your code EPM
Click OK

Then write this code into the Activate Worksheet module for this
sheet:-

:=======================

Private Sub Worksheet_Activate()

If ActiveSheet.ProtectContents = True Then
psw = InputBox("Please insert password to view data.", "Password
Checker")
If psw = "EPM" Then
Sheets("ABD").Unprotect ("EPM")
Else
MsgBox "Incorrect Password"
End If
End If

End Sub

:========================

Should work OK, let me know if there aare any problems

somethinglikeant
http://www.excel-ant.co.uk



NickHK[_3_]

Protecting Sheet
 
Assuming this code is on the sheet called "ABD", you need to reverse your
logic.
As you are in the _Activate event, that sheet is about to br shown, so you
have to do something to prevent it
You can't cancel this event, so you have to .Activate another sheet.
Private Sub Worksheet_Activate()
psw = InputBox("Please insert password to view data.", "Password Checker")
If psw = "EPM" Then
'Do nothing
Else
Worksheets(1).Activate
End If
End Sub

NickHK

"Nad" ...
Hi
I used this code to protect my sheet
Private Sub Worksheet_Activate()
psw = InputBox("Please insert password to view data.", "Password Checker")
If psw = "EPM" Then
Sheets("ABD").Select
Else
MsgBox "Incorrect Password"
End If
End Sub
But, even if i insert the wrong password my sheet become visible/active.
Pls tell me where i am wrong.
Thanks





All times are GMT +1. The time now is 04:27 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com