Warning: Undefined array key "HTTPS" in /customers/a/2/3/co93ul71s/webroots/r1107763/data/commonfunctions.php on line 287 Warning: Cannot modify header information - headers already sent by (output started at /customers/a/2/3/co93ul71s/webroots/r1107763/data/commonfunctions.php:287) in /customers/a/2/3/co93ul71s/webroots/r1107763/index.php on line 60 Erlandsen Data Consulting

Protect all cells containing formulas in a worksheet

 2009-12-17    Worksheets    0    173

The procedure below can be used to protect all cells containing formulas in a worksheet.

Sub ProtectFormulasInWS(ws As Worksheet)
' ws must be unprotected before running this procedure
' protect ws afterwards for the protection to take effect
Dim objRange As Range
    If ws Is Nothing Then Exit Sub
    
    With ws
        If .ProtectContents Then Exit Sub
        
        .Cells.Locked = False
        .Cells.FormulaHidden = False
        On Error Resume Next
        Set objRange = .UsedRange.SpecialCells(xlCellTypeFormulas)
        On Error GoTo 0
        If Not objRange Is Nothing Then
            With objRange
                .Locked = True
                .FormulaHidden = True
            End With
            Set objRange = Nothing
        End If
    End With
End Sub

Sub ExampleProtectFormulasInWS()
    ActiveSheet.Unprotect
    ProtectFormulasInWS ActiveSheet
    ActiveSheet.Protect
End Sub