SpecialCells
ロックされていないセルをすばやく識別するために使用する
以下のコード - QuickUnlocked - は回避策を使用してエラー セルのコレクションをすばやく生成しSpecialCells
、ロックされていないセル範囲を識別します。
主なコード手順は次のとおりです。
- を変更して、
Application
エラー、コード、および画面の更新を抑制します
ActiveWorkbook
および/またはActiveSheet
が保護されている場合は、ロックを解除しようとします。失敗した場合はコードを終了します
- 現在のシートの複製を作成
- を使用して、レプリカ内の既存の数式エラーを削除します。
SpecialCells
- レプリカ ワークシートを保護し、エラー処理の範囲で、ロックされていないセルのみを入力する意図的な数式エラーを追加します。
- 結果をクリーンアップして報告する アプリケーション設定をリセットする
SpecialCells
Xl2010以前は8192エリア限定の警告
この Microsoft KB の記事に従って、Excel-2007 以前のバージョンでは、VBA マクロを介して最大 8,192 個の連続していないセルがサポートされます。かなり驚くべきことに、VBA マクロを 8192 個を超えるSpecialCells Areas in these Excel versions, will not raise an error message, and the entire area under consideration will be treated as being part of the
SpecialCells の範囲コレクションに適用します。
クイックロック解除コード
Sub QuickUnlocked()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim lCalc As Long
Dim bWorkbookProtected As Boolean
On Error Resume Next
'test to see if WorkBook structure is protected
'if so try to unlock it
If ActiveWorkbook.ProtectStructure Then
ActiveWorkbook.Unprotect
If ActiveWorkbook.ProtectStructure Then
MsgBox "Sorry, I could not remove the passsword protection from the workbook" _
& vbNewLine & "Please remove it before running the code again", vbCritical
Exit Sub
Else
bWorkbookProtected = True
End If
End If
Set ws1 = ActiveSheet
'test to see if current sheet is protected
'if so try to unlock it
If ws1.ProtectContents Then
ws1.Unprotect
If ws1.ProtectContents Then
MsgBox "Sorry, I could not remove the passsword protection from sheet" & vbNewLine & ws1.Name _
& vbNewLine & "Please remove it before running the code again", vbCritical
Exit Sub
End If
End If
On Error GoTo 0
'disable screenupdating, event code and warning messages.
'set calculation to manual
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
lCalc = .Calculation
.Calculation = xlCalculationManual
End With
On Error Resume Next
'check for existing error cells
Set rng1 = ws1.Cells.SpecialCells(xlCellTypeFormulas, 16)
On Error GoTo 0
'copy the activesheet to a new working sheet
ws1.Copy After:=Sheets(Sheets.Count)
Set ws2 = ActiveSheet
'delete any cells that already contain errors
If Not rng1 Is Nothing Then ws2.Range(rng1.Address).ClearContents
'protect the new sheet
ws2.Protect
'add an error formula to all unlocked cells in the used range
'then use SpecialCells to read the unlocked range address
On Error Resume Next
ws2.UsedRange.Formula = "=NA()"
ws2.Unprotect
Set rng2 = ws2.Cells.SpecialCells(xlCellTypeFormulas, 16)
Set rng3 = ws1.Range(rng2.Address)
ws2.Delete
On Error GoTo 0
'if WorkBook level protection was removed then reinstall it
If bWorkbookProtected Then ActiveWorkbook.Protect
'cleanup user interface and settings
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
lCalc = .Calculation
End With
'inform the user of the unlocked cell range
If Not rng3 Is Nothing Then
MsgBox "The unlocked cell range in Sheet " & vbNewLine & ws1.Name & " is " & vbNewLine & rng3.Address(0, 0)
Else
MsgBox "No unlocked cells exist in " & ws1.Name
End If
End Sub