📊 Excel VBA Top100
📋

UnprotectSheet

シート管理

シートの保護を解除する

🎬 デモGIF準備中

📖 使い方

  1. VBAエディタを開く(Alt + F11)
  2. モジュールを挿入(挿入 > モジュール)
  3. 下記VBAコードをコピー&ペースト
  4. ブック上で実行(Alt + F8 でマクロ選択)
💡 実行例: 実行するとシート名とパスワード(設定されている場合)を入力するダイアログが表示される

💻 VBAコード

' UnprotectSheet
' -----------------
' Macro Name: UnprotectSheet
' Description: シートの保護を解除する
' Parameters: なし
' Returns: なし
' Usage: 実行するとシート名とパスワード(設定されている場合)を入力するダイアログが表示される
' -----------------

Sub UnprotectSheet()
    Dim ws As Worksheet
    Dim sheetName As String
    Dim password As String

    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

    If Not ws.ProtectContents Then
        MsgBox "「" & sheetName & "」は保護されていません。", vbInformation
        Exit Sub
    End If

    password = InputBox("パスワードを入力:", "シート保護解除")

    On Error Resume Next
    ws.Unprotect Password:=password
    On Error GoTo ErrorHandler

    If ws.ProtectContents Then
        MsgBox "パスワードが正しくありません。", vbExclamation
    Else
        MsgBox "「" & sheetName & "」の保護を解除しました。", vbInformation
    End If
    Exit Sub

ErrorHandler:
    MsgBox "エラーが発生しました: " & Err.Description, vbCritical
End Sub