Avatar of swimehdi
swimehdi
Flag for United States of America asked on

Protect sheets upon exiting .

I am interested to know if there is a possibility for my sheets to become protected automatically upon exiting the workbook
Template3.xls
Microsoft Excel

Avatar of undefined
Last Comment
swimehdi

8/22/2022 - Mon
swimehdi

ASKER
Thank you for the help
zorvek (Kevin Jones)

Add this code to the ThisWorkbook code module:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim Sheet As Sheet1
    For Each Sheet In ThisWorkbook.Sheets
        Sheet.Protect "Password"
    Next Sheet

End Sub

Kevin
swimehdi

ASKER
Thank you Kevin for the reply.
I am getting an error:

runtime error 13

Type mismatch.

File attached

Please help
Template4.xls
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
zorvek (Kevin Jones)

Fixed:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim Sheet As Sheet
    For Each Sheet In ThisWorkbook.Sheets
        Sheet.Protect "Password"
    Next Sheet

End Sub

Kevin
swimehdi

ASKER
I am getting a different error:  
"Compile Error:" "User-defined type not defined".  Thank you again
Template5.xls
ASKER CERTIFIED SOLUTION
zorvek (Kevin Jones)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
swimehdi

ASKER
Sweet!  It works like a charm

Thank you
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.