Hi,
I am struggling on how I can protect the Worksheet of my Workbook. In my Workbook, I have three worksheets namely, "Controls" and "SourceX".
The "SourceX" worksheet is hidden, so it is not currently my concern. My issue is with the worksheet "Controls", which I need to protect at all costs. It contains all the menu controls, with buttons, for different functionalities. I have tried to use the following VBA events for testing which one would work:
(1) "Workbook_SheetBeforeDelete(ByVal Sh As Object)" event from "Thisworkbook"
(2) "Worksheet_BeforeDelete()" event from the "Controls" sheet
Here are my codes for both approaches:
================================================================
(1) Workbook_SheetBeforeDelete
Private Sub Workbook_SheetBeforeDelete(ByVal Sh As Object)
Dim shPwd As String
Dim inPss As String
Dim shName As String
shName = "Controls"
If Sh.Name = shName Then
shPwd = "qwerty"
inPss = InputBox("Enter the password to delete the sheet")
If inPss = shPwd Then
'If the password is correct, allow the deletion
Else
'If the password is incorrect, prevent the deletion
MsgBox "Invalid credential. The sheet will not be deleted.", vbExclamation
Cancel = True 'Terminate the sheet deletion
End If
End If
End Sub
-----------------------------------------------------------------------------------------------------------
(2) Worksheet_BeforeDelete()
Private Sub Worksheet_BeforeDelete()
Dim shPwd As String
Dim inPss As String
shPwd = "qwerty"
If inPss = shPwd Then
'If the password is correct, allow the deletion
Else
'If the password is incorrect, prevent the deletion
MsgBox "Invalid credential. The sheet will not be deleted.", vbExclamation
Cancel = True 'Terminate the sheet deletion
End If
End Sub
================================================================
For some reason, these codes don't work. it complains about the "Cancel = True" statement and the code displays an error, and when terminated, it deletes the "Controls" sheet. I have even used "Sh.Delete" in replacement for "Cancel = True", but it just didn't work. Even if I tried to put an incorrect password, it loops in twice with the confirmation of deleting the sheet, and then an error window will appear. Pressing Debug or End, still deletes the "Controls" sheet.
So, I am reaching out to you, so you can give me some advise on how to resolve this or provide a different approach to prevent in deleting the said sheet.
Thank you very much!
I am struggling on how I can protect the Worksheet of my Workbook. In my Workbook, I have three worksheets namely, "Controls" and "SourceX".
The "SourceX" worksheet is hidden, so it is not currently my concern. My issue is with the worksheet "Controls", which I need to protect at all costs. It contains all the menu controls, with buttons, for different functionalities. I have tried to use the following VBA events for testing which one would work:
(1) "Workbook_SheetBeforeDelete(ByVal Sh As Object)" event from "Thisworkbook"
(2) "Worksheet_BeforeDelete()" event from the "Controls" sheet
Here are my codes for both approaches:
================================================================
(1) Workbook_SheetBeforeDelete
Private Sub Workbook_SheetBeforeDelete(ByVal Sh As Object)
Dim shPwd As String
Dim inPss As String
Dim shName As String
shName = "Controls"
If Sh.Name = shName Then
shPwd = "qwerty"
inPss = InputBox("Enter the password to delete the sheet")
If inPss = shPwd Then
'If the password is correct, allow the deletion
Else
'If the password is incorrect, prevent the deletion
MsgBox "Invalid credential. The sheet will not be deleted.", vbExclamation
Cancel = True 'Terminate the sheet deletion
End If
End If
End Sub
-----------------------------------------------------------------------------------------------------------
(2) Worksheet_BeforeDelete()
Private Sub Worksheet_BeforeDelete()
Dim shPwd As String
Dim inPss As String
shPwd = "qwerty"
If inPss = shPwd Then
'If the password is correct, allow the deletion
Else
'If the password is incorrect, prevent the deletion
MsgBox "Invalid credential. The sheet will not be deleted.", vbExclamation
Cancel = True 'Terminate the sheet deletion
End If
End Sub
================================================================
For some reason, these codes don't work. it complains about the "Cancel = True" statement and the code displays an error, and when terminated, it deletes the "Controls" sheet. I have even used "Sh.Delete" in replacement for "Cancel = True", but it just didn't work. Even if I tried to put an incorrect password, it loops in twice with the confirmation of deleting the sheet, and then an error window will appear. Pressing Debug or End, still deletes the "Controls" sheet.
So, I am reaching out to you, so you can give me some advise on how to resolve this or provide a different approach to prevent in deleting the said sheet.
Thank you very much!