ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Password Protecting/Unprotecting Via Code (https://www.excelbanter.com/excel-programming/347613-password-protecting-unprotecting-via-code.html)

MWS

Password Protecting/Unprotecting Via Code
 
I have a worksheet that has hidden rows and I'd like to write the code so
upon the user activating the worksheet, they are prompted for a password
and....

If the correct password is enterred, the rows become unhidden (that part is
not a problem).

And, if either an incorrect password OR the user selects "cancel" at the
password prompt, the process is aborted and the user is simply returned to
the worksheet and the rows remain unhidden (this section is my problem).
Currently, if the user inputs an incorrect password OR selects cancel, the
code fails.

The code I have is as follows:

Private Sub Worksheet_Activate()
ActiveSheet.Unprotect
Rows("2:5000").Select
Selection.EntireRow.Hidden = False
Range("F1").Select
End Sub

Can someone help by providing the additional code so the process executes as
desired?

Any and all help is appreciated - Thank You



Tom Ogilvy

Password Protecting/Unprotecting Via Code
 
Private Sub Worksheet_Activate()
ans = Inputbox("enter password")
if lcase(ans) < "mypasswordinlowercase" then exit sub
ActiveSheet.Unprotect
Rows("2:5000").Select
Selection.EntireRow.Hidden = False
Range("F1").Select
End Sub

--
Regards,
Tom Ogilvy

"MWS" wrote in message
...
I have a worksheet that has hidden rows and I'd like to write the code so
upon the user activating the worksheet, they are prompted for a password
and....

If the correct password is enterred, the rows become unhidden (that part

is
not a problem).

And, if either an incorrect password OR the user selects "cancel" at the
password prompt, the process is aborted and the user is simply returned to
the worksheet and the rows remain unhidden (this section is my problem).
Currently, if the user inputs an incorrect password OR selects cancel, the
code fails.

The code I have is as follows:

Private Sub Worksheet_Activate()
ActiveSheet.Unprotect
Rows("2:5000").Select
Selection.EntireRow.Hidden = False
Range("F1").Select
End Sub

Can someone help by providing the additional code so the process executes

as
desired?

Any and all help is appreciated - Thank You





eLiNK

Password Protecting/Unprotecting Via Code
 

Hi,

I don't know if there's a more effecient way of doing this
but here goes.

1) Create new UserForm (frmPassword) containing a textbox (txtPassword
and two command buttons ('OK' and 'Cancel').
2) Set the property 'PasswordChar' of the txtPassword to * (or whateve
you like more)
3) Write the necessary code for your 'OK' and 'Cancel' buttons.
4) Load your frmPassword user form in your Auro_open (Load frmPassword
frmPassword.Show)
5) Enjo

--
eLiN
-----------------------------------------------------------------------
eLiNK's Profile: http://www.hightechtalks.com/m44
View this thread: http://www.hightechtalks.com/t230446


MWS

Password Protecting/Unprotecting Via Code
 
Tom, Thank You for your response and I have a question regarding the
following suggested code:
Dim ans As String
ans = InputBox("Enter Password")
ActiveSheet.Unprotect
If LCase(ans) < "unhide" Then Exit Sub
Rows("2:5000").Select
Selection.EntireRow.Hidden = False
Range("F1").Select

When the file is opened and the sheet is activated, the InputBox is
presented and then the standard "Unprotect Sheet" prompt is displayed. The
standard prompt only appears after the inputbox when the file is first opened
and no time afterwards, but is there a way to avoid having the standard
prompt presented?

"Tom Ogilvy" wrote:

Private Sub Worksheet_Activate()
ans = Inputbox("enter password")
if lcase(ans) < "mypasswordinlowercase" then exit sub
ActiveSheet.Unprotect
Rows("2:5000").Select
Selection.EntireRow.Hidden = False
Range("F1").Select
End Sub

--
Regards,
Tom Ogilvy

"MWS" wrote in message
...
I have a worksheet that has hidden rows and I'd like to write the code so
upon the user activating the worksheet, they are prompted for a password
and....

If the correct password is enterred, the rows become unhidden (that part

is
not a problem).

And, if either an incorrect password OR the user selects "cancel" at the
password prompt, the process is aborted and the user is simply returned to
the worksheet and the rows remain unhidden (this section is my problem).
Currently, if the user inputs an incorrect password OR selects cancel, the
code fails.

The code I have is as follows:

Private Sub Worksheet_Activate()
ActiveSheet.Unprotect
Rows("2:5000").Select
Selection.EntireRow.Hidden = False
Range("F1").Select
End Sub

Can someone help by providing the additional code so the process executes

as
desired?

Any and all help is appreciated - Thank You







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

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