Protect/Unprotect worksheet VBA BUTTON?

s_macloskey

New Member
Joined
Jul 1, 2020
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hello all,

context:

I have created an employee holiday/leave sheet on (we use Excel 365)

I have made a sheet for each month and they require 'protecting' and 'unprotecting' seperately/individually - with a password

query:


My team is not so computer literate so, I need to create an easy to use Marco/VBA Button(s) which will protect (and unprotect) the worksheet and prompt for a password, the same way it would if you were to go to "Review, Protect worksheet"

I have tried creating a button and using VBA, and I've had a tiny amount success but I'm new to VBA and it's a bit of a mess.

*I've attached a screenshot for reference*

Can anyone provide any assistance?

Thanks,

S Macloskey
 

Attachments

  • leave diary.png
    leave diary.png
    110.7 KB · Views: 86

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You can change the sheet to active sheet or whatever the sheet name you want to unprotect
Set sh = activesheet
Set sh = Sheets("MySheet")

VBA Code:
Sub Unprotect_Sh()
    Dim sh As Worksheet
    Dim pw As String
    Dim inptBx As String

    Set sh = Sheets(2)
    pw = "1234"
    With sh
        inptBx = InputBox("Enter Pasword")
        If inptBx = pw Then
            .Unprotect pw
        Else
            MsgBox "Wrong Pasword"
        End If
    End With



End Sub
 
Upvote 0
You could use an active-x toggle button placed on the sheet to be protected
This alters the caption on the button when clicked
VBA Code:
Private Sub ToggleButton1_Click()
    Dim toggle As Object:   Set toggle = Me.ToggleButton1
    Const pw = "Password"
    If InputBox("Enter password", "") = pw Then
        Select Case toggle.Value
            Case True
                toggle.Caption = "Protect"
                Me.Unprotect pw
            Case False
                toggle.Caption = "Unprotect"
                Me.Protect pw
            End Select
    Else
        MsgBox "Bad password"
    End If
End Sub

Also consider:
The user may forget to protect the sheet
Perhaps the sheet should be auto-protected when user activates a different sheet or saves the workbook
If you need help with that, let us know :)
 
Upvote 0
You could use an active-x toggle button placed on the sheet to be protected
This alters the caption on the button when clicked
VBA Code:
Private Sub ToggleButton1_Click()
    Dim toggle As Object:   Set toggle = Me.ToggleButton1
    Const pw = "Password"
    If InputBox("Enter password", "") = pw Then
        Select Case toggle.Value
            Case True
                toggle.Caption = "Protect"
                Me.Unprotect pw
            Case False
                toggle.Caption = "Unprotect"
                Me.Protect pw
            End Select
    Else
        MsgBox "Bad password"
    End If
End Sub

Also consider:
The user may forget to protect the sheet
Perhaps the sheet should be auto-protected when user activates a different sheet or saves the workbook
If you need help with that, let us know :)


Wow! Thank you so much for this. It works perfectly.
Auto-protecting when switching between sheets would be a good idea too and I would need help with that, nonetheless this is the perfect solution.

Thank you,

S Macloskey
 
Upvote 0
Auto-protecting when switching between sheets would be a good idea

Code below auto-protects the sheet when user clicks on a different sheet
It must be placed in the sheet's own code window as detailed below

Right click on sheet tab \ View Code \ paste code below into the window that opens \ amend the password
VBA Code:
Private Sub Worksheet_Deactivate()
    Me.Protect "Password"
End Sub
 
Upvote 0
Solution
Code below auto-protects the sheet when user clicks on a different sheet
It must be placed in the sheet's own code window as detailed below

Right click on sheet tab \ View Code \ paste code below into the window that opens \ amend the password
VBA Code:
Private Sub Worksheet_Deactivate()
    Me.Protect "Password"
End Sub

This works very well, thank you Yongle. That's really helpful.
 
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,665
Members
452,992
Latest member
TokugawaIesuma

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