If worksheet is protected display msgbox and complete an action, else display a different msgbox and exit sub

rdoulaghsingh

Board Regular
Joined
Feb 14, 2021
Messages
105
Office Version
  1. 365
Platform
  1. Windows
I wrote the code below for a lock and unlock process. First one (Lock) is supposed to state if the worksheet (Change Record) is already locked then show that it's already protected with a timed message box....else (if false) then lock the worksheet.

The second (Unlock) is doing the opposite. If the worksheet is locked then display a message box for confirmation and if yes is selected then unlock the worksheet....else, if false, meaning the worksheet is unlocked already, then display a msgbox saying it's already unlocked.

I've tested this a few different ways, but the msgbox seems to popup incorrectly whether it's locked or unlocked.

Please help!


VBA Code:
Sub CR_Lock()
Dim msg1Counter As Integer

If Worksheets("Change Record").Protect = True Then

msg1Counter = CreateObject("WScript.Shell").PopUp(ActiveSheet.Name & " Worksheet is already protected.", 1, "Info.")

Else

msg1Counter = CreateObject("WScript.Shell").PopUp("Hang tight!" & vbCrLf & vbCrLf & "Initiating protection for " & ActiveSheet.Name & " Worksheet.", 2, "Protection in progress")

Worksheets("Change Record").Protect

    End If

End Sub
Sub CR_Unlock()
Dim msg2Counter As Integer

If Worksheets("Change Record").Protect = True Then

answer = MsgBox("Unlock " & ActiveSheet.Name & " Worksheet?", vbQuestion + vbYesNo + vbDefaultButton2, "Unlock Worksheet confirmation")

    If answer = vbYes Then
    
Application.ScreenUpdating = False

Worksheets("Change Record").Unprotect

    End If

Else

msg2Counter = CreateObject("WScript.Shell").PopUp(ActiveSheet.Name & " protection is currently turned off.", 1, "Info.")
    
    End If

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
It needs to be
VBA Code:
If Worksheets("Change Record").ProtectContents = True Then
your code is actually protecting the sheet
 
Upvote 0
Solution
It needs to be
VBA Code:
If Worksheets("Change Record").ProtectContents = True Then
your code is actually protecting the sheet
I literally just found it on the internet and tested it and it worked. Thanks for your quick reply.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top