[Excel VBA] How to Protect and Unprotect Worksheets

目次

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.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次