Avatar of philosopher76
philosopher76
Flag for Mexico asked on

How to lock/unlock a worksheet/workbook with a defined password using a macro in excel?

I would like to save time opening and closing my file and I would like to run a macro to do so.

Thanks!
Microsoft Excel

Avatar of undefined
Last Comment
Roy Cox

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Roy Cox

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Roy Cox

To re-protect the sheets when closing place this code in the WorkBook close event

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
 Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
        ws.Protect "YourPassworsd"
    Next ws

End Sub

Open in new window


The workbook will still have it's protection unless you remove it
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck