アプリ版:「スタンプのみでお礼する」機能のリリースについて

Excelファイルのすべてのシートにpwで一括保護、解除できるマクロを教えてください。同ファイル内のすべてのシートに同じpwを使用します。
パスワードはファイルにより異なるのでメッセージボックスか何かでpwを入力させる命令が必要かと思います。

A 回答 (1件)

こんばんは、


>マクロを教えてください
どこまでをどのように伝えれば良いのかわかりませんが、
コードを書いておきます。
何かで、なので UserFormを使用した例です。
呼び出しSampleは標準モジュールで
UserFormは添付図のような形です。
すべてのシートが同じ状態である事(保護又は非保護)
標準モジュール
Option Explicit
Sub Sample()
Dim cnt As Integer
Dim ws As Worksheet
Dim Ufm_caption As String, Btn_caption As String
For Each ws In Worksheets
  If ws.ProtectContents = True Then
   cnt = cnt + 1
   Ufm_caption = "Unlock with Password"
   Btn_caption = "UnProtect"
  Else
   Ufm_caption = "Lock with Password"
   Btn_caption = "Protect"
  End If
Next
If cnt > 0 And cnt < Worksheets.Count Then
  MsgBox ("Is some sheet protected?")
  Exit Sub
Else
  UserForm1.Caption = Ufm_caption
  UserForm1.CommandButton1.Caption = Btn_caption
  UserForm1.Show
End If
End Sub

Formモジュール
Option Explicit
Private Sub UserForm_Initialize()
TextBox1.PasswordChar = "*"
End Sub
Private Sub CommandButton1_Click()
Dim flag As Boolean
Dim ws As Worksheet
Dim msg As String
If CommandButton1.Caption = "Protect" Then
  flag = True
  msg = "Set the sheet protection"
Else
  msg = "Released the sheet protection"
End If
For Each ws In Worksheets
  On Error GoTo myErr
  If flag = True Then
   ws.Protect Password:=TextBox1.Value
  Else
   ws.Unprotect Password:=TextBox1.Value
  End If
Next
MsgBox msg
Unload Me
Exit Sub
myErr:
MsgBox ("The password is incorrect !")
End Sub

以前作ったモノを改造しました。
一応、検証済み
「Excelファイルのすべてのシートにpw」の回答画像1
    • good
    • 0
この回答へのお礼

すいません。マクロの基本の基本が分かっていません。標準モジュールにすべて貼りつける程度しか分かりません。Formモジュールとはどのように扱うのでしょうか。

お礼日時:2021/03/18 20:45

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!