保护Excel工作表中的所有公式

 

保护Excel工作表中的所有公式保护Excel工作表中的所有公式

Sub 保护公式()   '对活动工作簿中所有未保护的工作表生效
    Dim Item As Integer                      '声明变量
    On Error Resume Next                     '有错误时继续执行下一步
    For i = 1 To Worksheets.Count            '遍历所有工作表(不能使用Sheets)
      If Not Worksheets(i).ProtectContents Then '如果工作表未保护
         Worksheets(i).UsedRange.Locked = False        '解除已用数据区域的锁定属性
         Worksheets(i).UsedRange.FormulaHidden = False '解除已用数据区域的隐藏公式属性
           With Worksheets(i).UsedRange.SpecialCells(xlCellTypeFormulas, 23)  '引用对公式区域
             .Locked = True                            '锁定单元格
             .FormulaHidden = True                     '隐藏公式
             '保护工作表,除密为空,而且允许一切操作,避免影响其它操作
             Worksheets(i).Protect Password:="", DrawingObjects:=False, _
             Contents:=True, Scenarios:=False, AllowFormattingCells:=True, _
             AllowFormattingColumns:=True, AllowFormattingRows:=True, _
             AllowInsertingColumns:=True, AllowInsertingRows:=True, _
             AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
             AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
             AllowUsingPivotTables:=True
          End With
       End If
    Next i
End Sub