30 Advanced Excel Tips for 2026

Excel Champs / VBA / VBA Protect/Unprotect Workbook (Excel File)
By Puneet Gogia (Microsoft MVP)

VBA Protect/Unprotect Workbook (Excel File)

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

ThisWorkbook.Protect Password:="test123", Structure:=True protects the workbook’s structure: nobody can add, delete, rename, move, hide or unhide sheets without the password. ThisWorkbook.Unprotect Password:="test123" removes the protection. The password is case-sensitive.

Workbook protection doesn’t lock cells. To stop changes to the data, protect each sheet as well, as in Worksheets("Data").Protect Password:="test123". To require a password to open the file, set ThisWorkbook.Password = "open123" and save the workbook.

To protect a workbook that’s closed, open it, protect it and save it as it closes:

Dim wb As Workbook
Set wb = Workbooks.Open("C:ReportsSales.xlsx")
wb.Protect Password:="test123", Structure:=True
wb.Close SaveChanges:=True

Write SaveChanges:=True with the colon. Without it, VBA reads SaveChanges = True as a comparison and passes False, so the file closes without saving the protection. Anyone who can open the VBA project can read a password typed into the code, so lock the project too, under Tools → VBAProject Properties → Protection.

To protect a workbook, you need to use VBA’s PROTECT method. By using this method, you can use a password, or you can activate workbook protection without a password. Workbook protection locks the structure of the workbook, so nobody can add, delete, move, rename, hide or unhide its sheets.

But there’s one thing that you need to take care of: the password that you use is CASE-SENSITIVE.

Workbook.Protect "Password"
  1. Specify the workbook that you want to protect.
  2. Type a dot, and select the “Protect” method from the list, or you can type “Protect”.
  3. Enter the password that you want to set.
  4. Run the code to protect the workbook.

In this tutorial, we will learn this method in different ways, and we will also learn to unprotect a workbook.

Correct Way to Remove Password from Protected Worksheet

Protect a Workbook with a Password

Below is the code that you need to use to protect a workbook with a password. In this code, you have the workbook name first and the protection method with the password.

Workbooks("Book1").Protect "test123"

And when I run this code, it locks the “Book1” workbook.

it locks the book1 workbook

Protect a Workbook without a Password

And if you want to protect a workbook without a password, you need to skip the “Password” argument, just like the following code.

Workbooks("Book1").Protect

If you are trying to protect a workbook without a password that is already protected with a password, Excel will show a dialog box to enter that password.

Unprotect a Workbook with a Password

In the same way, you can use the unprotect method to unprotect the workbook, and the code for this would be something like below.

Workbooks("Book1").Unprotect "test123"

There’s one thing that you need to take care of: if you try to unprotect a workbook with the wrong password, VBA shows run-time error 1004, and the workbook stays protected.

Unprotect a Workbook without a Password

And if a workbook is protected without a password, you can use the same method without specifying the password argument.

Protect All the Open Workbooks

In the code below, we have a FOR EACH loop to loop through all the open workbooks and protect each workbook one by one using the same protection method.

Sub vba_protect_all_open_workbooks()
'variable to use as a workbook
Dim wb As Workbook

'loop through all the open workbooks
For Each wb In Application.Workbooks

  'condition to check workbook name
  If wb.Name <> "PERSONAL.XLSB" Then

    'protect every workbook with a password
     wb.Protect "test123"

  End If

Next wb

End Sub

As I use the personal macro workbook, I have used an IF THEN ELSE statement to skip if the workbook name is “personal.xlsb”. Make sure to take care of that as well.

Protect a Closed Workbook

You can also protect and unprotect a workbook that is closed and saved in a location. Look at the following code, where I have used the location of my desktop to use the file “test.xlsx”.

Sub vba_protect_workbook()

'declare variable to use for the workbook
Dim wb As Workbook

'open the workbook that you want to protect
Set wb = Workbooks.Open("C:\Users\Dell\Desktop\test.xlsx")

'turn off the screen-updating to _
done everything in the backend
Application.ScreenUpdating = False

'protect the workbook
wb.Protect

'close the workbook after protecting
wb.Close SaveChanges:=True
'turn on the screen-updating
Application.ScreenUpdating = True

End Sub

The above code uses the Workbooks.Open method to open the workbook that you want to protect, and then disables screen updating to work in the background. In the end, it closes the workbook with SaveChanges:=True, so the protection is saved in the file.

Protect All the Closed Workbooks from a Folder

The following code loops through all the workbooks in the folder you specified as the path. It then opens each workbook, protects it, and closes it one by one.

Sub vba_protect_all_the_workbooks()

'declare variables
    Dim wb As Workbook
    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String
    Dim FldrPicker As FileDialog

'optimize macro
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

'specify the folder where workbooks are saved
    myPath = "C:\Users\Dell\Desktop\folder\"

'jump to reset setting if there's an error
    On Error GoTo ResetSettings

'target file extension (must include wildcard "*")
    myExtension = "*.xls*"

'target path
    myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
    Do While myFile <> ""

        'Set variable equal to opened workbook
            Set wb = Workbooks.Open(Filename:=myPath & myFile)

        'protect workbook
            wb.Protect

        'Save and Close Workbook
            wb.Close SaveChanges:=True

        'Get next file name
            myFile = Dir

    Loop

'Message Box when task is completed
  MsgBox "Done!"

'reset setting that were change for optimization
ResetSettings:
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

Protect Arguments: Password, Structure and Windows

The Protect method has three optional arguments:

  • Password: the password to protect the workbook. It’s case-sensitive, and if you leave it out, anyone can unprotect the workbook.
  • Structure: True to protect the structure (the sheets). This is the default.
  • Windows: True to protect the workbook windows. This option only works in Excel 2010 and earlier and in Excel for Mac; newer versions on Windows ignore it.
Sub protect_structure()
ThisWorkbook.Protect Password:="test123", Structure:=True
End Sub

Check If a Workbook Is Protected

Use the ProtectStructure property to check if a workbook’s structure is protected before you protect or unprotect it:

Sub check_protection()
If ThisWorkbook.ProtectStructure Then
MsgBox "The workbook is protected."
Else
MsgBox "The workbook is not protected."
End If
End Sub

Require a Password to Open the Workbook

Protecting a workbook doesn’t stop anyone from opening it or reading the data. If you want a password to open the file, save it with the Password argument of SaveAs, which encrypts the file:

Sub save_with_open_password()
ThisWorkbook.SaveAs Filename:=ThisWorkbook.FullName, Password:="test123"
End Sub

If you run this on a workbook that’s already saved, Excel asks if you want to replace the existing file. And to protect the cells on a sheet, use the worksheet’s Protect method instead (see VBA Protect Sheet).

Frequently Asked Questions

What is the difference between protecting a workbook and a worksheet?

Workbook protection locks the structure, so sheets can’t be added, deleted, moved or renamed. Worksheet protection locks the cells, so their values and formatting can’t be changed. You can use both together.

Can a macro still change a protected workbook?

Not the structure. A macro that adds or deletes a sheet gives an error until it unprotects the workbook first. Unprotect it at the start of the macro and protect it again at the end.

Is workbook protection secure?

It stops accidental changes, but it isn’t strong security. Don’t rely on it for sensitive data; use a password to open the file (encryption) instead.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel