ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   protect sheet (https://www.excelbanter.com/excel-programming/370636-protect-sheet.html)

enyaw

protect sheet
 
I need to password protect the worksheet when the userform is closed and
unprotect it when the userform is open. Can anyone help me???

DartGuru

protect sheet
 

enyaw wrote:
I need to password protect the worksheet when the userform is closed and
unprotect it when the userform is open. Can anyone help me???


How about....

Call ProtectSheet(False, "Sheet1")
frmUserForm.Show 1
... do whatever provessing the form requires ...
Call ProtectSheet(True, "Sheet1")

Private Sub ProtectSheet(bProtect As Boolean, sSheet As String)

If bProtect Then
Worksheets(sSheet).Protect Password:="<your password",
AllowFiltering:=True
Else
Worksheets(sSheet).Unprotect ("<your password")
End If

End Sub



I use a string variable to hold the password, and ProtectSheet because
I can use it for different worksheets, but you can just as easily use
just the code within the IF statement if you wish.


HTH


DartGuru

protect sheet
 

enyaw wrote:
I need to password protect the worksheet when the userform is closed and
unprotect it when the userform is open. Can anyone help me???


How about....

Call ProtectSheet(False, "Sheet1")
frmUserForm.Show 1
... do whatever provessing the form requires ...
Call ProtectSheet(True, "Sheet1")

Private Sub ProtectSheet(bProtect As Boolean, sSheet As String)

If bProtect Then
Worksheets(sSheet).Protect Password:="<your password",
AllowFiltering:=True
Else
Worksheets(sSheet).Unprotect ("<your password")
End If

End Sub



I use a string variable to hold the password, and ProtectSheet because
I can use it for different worksheets, but you can just as easily use
just the code within the IF statement if you wish.


HTH



All times are GMT +1. The time now is 06:58 AM.

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