How to protect and unprotect sheets of an Excel workbook using macro

How to create the Macro in Excel in a few steps

Before explaining how to protect or unprotect an Excel workbook with macros, you should know how to create macros. The procedure is very simple and we will explain you step by step so that you can do it without any inconvenience and then you can create an Excel file for read only.

To start you need to open the Excel program and go to the editor of Visual Basic. You can see it at the top of the screen, on your left. It is usually represented by VS.




How to protect and unprotect sheets of an Excel workbook using macro

Here is an important clarification. It depends on the version of Excel you have available, the path will vary. If you don't have the VB button in the toolbar at the top of the screen, do the following: «File> options> Developer / Programmer> Virtual Basic»

The editor will open automatically and you can use the menus and tools in the window. You must enter the following command inside a new form: ActiveSheet and in brackets a password with which we want to establish the protection, you can also add the name or number of the sheet if you want to specify it, or you can also protect cells in Excel in so that they are not changed.

How it works and what is Macro for Excel

A macro automates the functions we frequently perform to optimize editing time for a workbook in Excel. They are created with Microsoft VirtualBasic and are designed by software developers.

With it we can insert a password in an Excel file so that other users cannot modify, copy or print the data available within that document. Therefore, they can only log in with a read mode, unless the password is entered to enable the change.




The first thing to do is to allow access to the object model of the VBA project via a password. To do this, perform the following sequence «File> Options> Trust Center> Trust Center Settings> Macro Settings. There you have to select «Trusted access to the object model of VBA projects.

How to protect and unprotect sheets of an Excel workbook using macro

To protect a book, you must also select the box » Enable all macros ». In case you want to deprotect them, you should instead select "Disable all macros with or without notification".

How to protect an Excel workbook with macros in just a few steps

To protect the structure of the workbook so that no one can create or delete sheets unless they know the password, you need to write the following command in VBA:

[codesyntax lang = »vb» lines = »no» container = »pre» blockstate = »expanded» doclinks = »0 ″] 

And then it will be written as follows:

Secondary protection ()

If you make a mistake, go to end

        ActiveWorkbook.Protect ("Password to protect")

fine:

End subtitle

If you want to protect the entire book, you have to write the following command

[codesyntax lang = »vb» lines = »no» container = »pre»]

And then it should be established as follows

Sub Protect (name of the book or page)

    If you make a mistake, go to end

    Dim sht as a worksheet


    Application.ScreenUpdating = False


        ActiveWorkbook.Protect ("password to protect the book")


        For everything in ActiveWorkbook.Worksheets

            Se sht.Visible = True Allora

                sht.Protect ("password to protect the book")


            End if


        Next

fine:

End subtitle

As you can see, we have added rows in which we discriminate the sheets we want to protect with the Protect command and the name or number of the sheet.

How to check an Excel workbook in just a few steps

You can unprotect an Excel workbook or spreadsheet the same way you protected, only we'll change some command details.


To unprotect a spreadsheet we will insert the following code:

ActiveSheet.Unprotect "Password you used to protect"

If instead what we want to do is deprotect an Excel workbook, we have to insert the following code:

ThisWorkbook.Unprotect «Password you used to protect»

We hope you found this tutorial on safety and security in Excel useful. If you have any questions, leave them in the comments so we can help you resolve it.


add a comment of How to protect and unprotect sheets of an Excel workbook using macro
Comment sent successfully! We will review it in the next few hours.