VBA Code to Protect Worksheet with Password on Close

VBA Code to Protect Worksheet with Password on Close

Table of Contents

Password Protect Worksheet on Close with VBA (with Images)

What is it:

When you want others to only view the workbook data and no editing should be permitted then you have to make sure you protect the workbook with password while closing it. So that even if someone tries to edit the data it will ask for the password unless they input the password data will not be editable.

The manual way of doing the same is

ReviewProtect Sheet and then Save workbook.

Password Protect

Why is it:

If you make frequent updates in the workbook then Every time when you make changes in the workbook you have to make sure you protect it while closing. Some day if you forget to protect then the data will be editable by others.

The below VBA code will help you to protect the workbook automatically while closing the workbook. That means you don’t have to protect it manually everything when you make changes in the workbook.

How to password protect with VBA upon Close:

Step 1: Open the workbook that need to be protected and press Alt + F11

Step 2: Copy and Paste the below code in the VBA editor window and Save the workbook.

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Sheets(“Sheet1″).Protect Password:=”Your Password Here”   ‘Replace “Sheet1” with your sheet name and “Your Password Here” with the password of your choice.


End Sub

Note: Workbook should be saved in a macro enabled format (.xlm, .xlsm, .xlsb etc.) also make sure you protect the VBA code as well. 🙂

That’s It Now Onward your Data will be Safe Always!!

Anson Antony

Anson Antony

Anson is a contributing author and founder at ASKEYGEEK.com. Learning anything new has always been his passion, ASKEYGEEK.com is an outcome of his passion for technology and business. He has got a decade of versatile experience in Business Process Outsourcing, Finance & Accounting, Information Technology, Operational Excellence & Business Intelligence. During the tenure, he had worked for organizations like Genpact, Hewlett Packard, M*Modal and Capgemini in various roles and responsibilities. Outside business and technology, he is a movie buff who spends hours together watching and learning Cinema and a Film Maker too!

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *

You Made It,
Don't Close!

Enter your chance to win 100,000 UberTTS Character Credits
Thank you for your visit!

This popup won’t show up to you again!!!

Premium Membership Giveaway - Popup
Share to...