Filled Cells
私が仮定することにより、Cell はFormulas
またはConstants
またはを持っていComments
ます。
コードに組み込むことができるこの例を参照してください。これはすべてのセルをループするのではなく、SpecialCells
未テスト
Sub Sample()
Dim Rng As Range
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
.Cells.Locked = False
On Error Resume Next
Set Rng = .Cells.SpecialCells(xlCellTypeConstants)
Rng.Locked = True
Set Rng = .Cells.SpecialCells(xlCellTypeFormulas)
Rng.Locked = True
Set Rng = .Cells.SpecialCells(xlCellTypeComments)
Rng.Locked = True
On Error GoTo 0
End With
End Sub
すべてのワークシートに適用する場合は、すべてのワークシートをループするだけです
例えば
Sub Sample()
Dim Rng As Range
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
.Cells.Locked = False
On Error Resume Next
Set Rng = .Cells.SpecialCells(xlCellTypeConstants)
Rng.Locked = True
Set Rng = .Cells.SpecialCells(xlCellTypeFormulas)
Rng.Locked = True
Set Rng = .Cells.SpecialCells(xlCellTypeComments)
Rng.Locked = True
On Error GoTo 0
End With
Next
End Sub
フォローアップ(コメントから)
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Rng As Range
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
.UnProtect
.Cells.Locked = False
On Error Resume Next
Set Rng = .Cells.SpecialCells(xlCellTypeConstants)
Rng.Locked = True
Set Rng = .Cells.SpecialCells(xlCellTypeFormulas)
Rng.Locked = True
Set Rng = .Cells.SpecialCells(xlCellTypeComments)
Rng.Locked = True
On Error GoTo 0
.Protect
.EnableSelection = xlUnlockedCells
End With
Next
End Sub