Adding A Password to a Macro

scott_86_

New Member
Joined
Sep 27, 2018
Messages
40
Office Version
  1. 365
Platform
  1. Windows


Hi, I have two separate macros that work correctly withbuttons in the quick access toolbar to execute each independently.

They are 'Protect All Worksheets' & 'Unprotect All Worksheets.'

Can someone please help me to add a password just to the code 'Unprotect AllWorksheets' so that when it's button is pressed a password must beentered before executing.

Also, does anyone have a code to automatically 'Protect All Worksheets'when closing a workbook? (No password is needed here.)

For some reason I cannot attach an example file from my work computer. Thanksin advance.

Sub Unprotect_All()
Dim ws As Worksheet
On Error Resume Next
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
ws.UnprotectPassword:="wts"
Next ws
Application.ScreenUpdating = True
On Error GoTo 0
End Sub

 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,
see if this update to your code does what you want


Code:
Sub Unprotect_All()
    Dim ws As Worksheet
    Dim ProtectPassword As Variant
    
    On Error GoTo myerror
    
    Do
    ProtectPassword = InputBox("Enter Protection Password", "Password")
'cancel pressed
    If StrPtr(ProtectPassword) = 0 Then Exit Sub
    Loop Until Len(ProtectPassword) > 0
    
    For Each ws In ActiveWorkbook.Worksheets
        ws.Unprotect Password:=ProtectPassword
    Next ws
    
myerror:
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub


You can use the Workbook_BeforeClose event to place code to protect sheets & save changes.


Untested but something like following maybe

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim ws As Worksheet
'if changes made then apply sheet protection & save
    If Not Me.Saved Then
        For Each ws In ActiveWorkbook.Worksheets
            ws.Protect Password:="passwordhere"
        Next ws
        
        ThisWorkbook.Save
    End If


End Sub

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,740
Messages
6,180,679
Members
452,993
Latest member
FDARYABEE

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