目次
Introduction
I wanted to automate the process of protecting and unprotecting sheets using VBA, so I learned how to do it.
Solution: Use Protect and Unprotect
In Excel VBA, you can protect or unprotect a sheet with a single line of code.
Code to Protect a Sheet
ActiveSheet.Protect
Code to Unprotect a Sheet
ActiveSheet.Unprotect
Just by running these commands, protection or unprotection is executed for the currently active sheet.
Setting a Password
You can also set a password when protecting or unprotecting a sheet.
Protecting a Sheet with a Password
ActiveSheet.Protect Password:="morimorimori"
Unprotecting a Sheet with a Password
ActiveSheet.Unprotect Password:="morimorimori"
Please change the password string (here, "morimorimori") to any password you need.
Summary
In VBA, you can easily control sheet protection using Protect and Unprotect. By specifying a password, you can ensure more secure operations.
