VBAでマクロを操作する際、シートが保護されているかどうかを調べることはよくあります。しかし、単に保護されているか(True
/False
)を知るだけでなく、「具体的にどの操作が許可されているのか」を詳細に知りたい場合もあります。
例えば、「この保護されたシートではオートフィルタの利用が許可されているか?」といった条件をマクロで判定できれば、より高度で安定した処理を組むことが可能になります。
この記事では、Worksheet
オブジェクトとその.Protection
オブジェクトを使い、シートの保護に関する詳細な設定項目を一つずつ確認する方法を解説します。
完成したVBAコード
以下が、現在アクティブなシートの保護設定の詳細を調べ、結果を一つのメッセージボックスにまとめて表示するVBAコードです。
Sub CheckSheetProtectionDetails()
' 変数を宣言します
Dim targetSheet As Worksheet
Dim protectionInfo As String
' 現在アクティブなシートを操作対象とする
Set targetSheet = ActiveSheet
' --- 1. 基本的な保護状態を確認 ---
protectionInfo = "【シート保護の詳細設定】" & vbCrLf & vbCrLf
protectionInfo = protectionInfo & "■ 基本状態" & vbCrLf
protectionInfo = protectionInfo & "セルの内容が保護されているか (.ProtectContents): " & targetSheet.ProtectContents & vbCrLf
protectionInfo = protectionInfo & "図形オブジェクトが保護されているか (.ProtectDrawingObjects): " & targetSheet.ProtectDrawingObjects & vbCrLf
protectionInfo = protectionInfo & "UIのみの保護か (.ProtectionMode): " & targetSheet.ProtectionMode & vbCrLf
' --- 2. 許可されている操作の詳細を確認 ---
' .Protection オブジェクトを介して詳細設定にアクセスします
If targetSheet.ProtectContents = True Then
With targetSheet.Protection
protectionInfo = protectionInfo & vbCrLf & "■ 許可されている操作" & vbCrLf
protectionInfo = protectionInfo & "セルの書式設定 (.AllowFormattingCells): " & .AllowFormattingCells & vbCrLf
protectionInfo = protectionInfo & "列の書式設定 (.AllowFormattingColumns): " & .AllowFormattingColumns & vbCrLf
protectionInfo = protectionInfo & "行の書式設定 (.AllowFormattingRows): " & .AllowFormattingRows & vbCrLf
protectionInfo = protectionInfo & "列の挿入 (.AllowInsertingColumns): " & .AllowInsertingColumns & vbCrLf
protectionInfo = protectionInfo & "行の挿入 (.AllowInsertingRows): " & .AllowInsertingRows & vbCrLf
protectionInfo = protectionInfo & "ハイパーリンクの挿入 (.AllowInsertingHyperlinks): " & .AllowInsertingHyperlinks & vbCrLf
protectionInfo = protectionInfo & "列の削除 (.AllowDeletingColumns): " & .AllowDeletingColumns & vbCrLf
protectionInfo = protectionInfo & "行の削除 (.AllowDeletingRows): " & .AllowDeletingRows & vbCrLf
protectionInfo = protectionInfo & "並べ替え (.AllowSorting): " & .AllowSorting & vbCrLf
protectionInfo = protectionInfo & "オートフィルター (.AllowFiltering): " & .AllowFiltering & vbCrLf
End With
End If
' --- 3. 結果を表示 ---
MsgBox protectionInfo, vbInformation, "シート保護設定の確認"
End Sub
コードのポイント解説
基本的な保護状態のプロパティ
まず、シートオブジェクトが直接持つ、基本的な保護状態を示すプロパティがあります。
.ProtectContents
:True
の場合、ロックされたセルの内容が保護されています。これがシート保護の最も基本的な状態です。.ProtectDrawingObjects
:True
の場合、図形オブジェクトが保護されています。.ProtectionMode
:True
の場合、UserInterfaceOnly
引数を指定した特殊な保護(ユーザーの操作のみを禁止し、マクロの操作は許可する)が有効であることを示します。
詳細な許可設定: .Protection
オブジェクト
「セルの書式設定を許可する」「並べ替えを許可する」といった、保護ダイアログのチェックボックスに対応する詳細な設定は、Worksheet
オブジェクトが持つ.Protection
オブジェクトの中に格納されています。
VB.Net
With targetSheet.Protection
... .AllowFiltering ...
... .AllowSorting ...
End With
mySheet.Protection.AllowFiltering
のように、.Protection
を介して各許可設定のTrue
/False
を調べることができます。
▼ 主な許可設定のプロパティ
.AllowFormattingCells
: セルの書式設定.AllowInsertingColumns
: 列の挿入.AllowDeletingRows
: 行の削除.AllowSorting
: 並べ替え.AllowFiltering
: オートフィルター.AllowUsingPivotTables
: ピボットテーブルの操作
これらのプロパティはすべて読み取り専用です。VBAからこれらの許可設定を変更したい場合は、一度シートの保護を解除(Unprotect
)し、再度Protect
メソッドを実行する際に引数として指定する必要があります。
まとめ
シートの保護に関する詳細な設定内容は、2段階で確認します。
- まず、
.ProtectContents
などで、シートが保護されているかを大まかに確認する。 - 保護されている場合は、
.Protection
オブジェクトを介して、**個別の許可設定(.AllowFiltering
など)**がTrue
かFalse
かを調べる。
この方法を使えば、「オートフィルターが許可されている場合のみ、フィルター操作を行う」といった、シートの保護状態に応じた、より賢く、より安定したマクロを構築することが可能になります。