📋
ProtectSheet
シート管理シートを保護する(編集禁止)
🎬 デモGIF準備中
📖 使い方
- VBAエディタを開く(Alt + F11)
- モジュールを挿入(挿入 > モジュール)
- 下記VBAコードをコピー&ペースト
- ブック上で実行(Alt + F8 でマクロ選択)
💡 実行例: 実行するとシート名とパスワードを入力するダイアログが表示される
💻 VBAコード
' ProtectSheet
' -----------------
' Macro Name: ProtectSheet
' Description: シートを保護する(編集禁止)
' Parameters: なし
' Returns: なし
' Usage: 実行するとシート名とパスワードを入力するダイアログが表示される
' -----------------
Sub ProtectSheet()
Dim ws As Worksheet
Dim sheetName As String
Dim password As String
Dim usePassword As VbMsgBoxResult
On Error GoTo ErrorHandler
sheetName = InputBox("保護するシート名を入力:", "シート保護", ActiveSheet.Name)
If sheetName = "" Then
Exit Sub
End If
On Error Resume Next
Set ws = Worksheets(sheetName)
On Error GoTo ErrorHandler
If ws Is Nothing Then
MsgBox "「" & sheetName & "」は存在しません。", vbExclamation
Exit Sub
End If
usePassword = MsgBox("パスワードを設定しますか?", vbYesNoCancel + vbQuestion, "シート保護")
If usePassword = vbCancel Then
Exit Sub
ElseIf usePassword = vbYes Then
password = InputBox("パスワードを入力:", "シート保護")
If password = "" Then
MsgBox "パスワードが空です。保護をキャンセルします。", vbInformation
Exit Sub
End If
End If
If password <> "" Then
ws.Protect Password:=password
Else
ws.Protect
End If
If usePassword = vbYes Then
MsgBox "「" & sheetName & "」をパスワード付きで保護しました。", vbInformation
Else
MsgBox "「" & sheetName & "」を保護しました。", vbInformation
End If
Exit Sub
ErrorHandler:
MsgBox "エラーが発生しました: " & Err.Description, vbCritical
End Sub