Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)Dim sht As Worksheet, rC As RangeFor Each sht In Sheets sht.Unprotect With sht For Each rC In .UsedRange If rC.FormulaLocal <> "" Then rC.Locked = True Else rC.Locked = False End If Next End With sht.ProtectNextEnd Sub
在beforesave事件中遍歷usedRange,把每個formula不為空的單元格加鎖
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)Dim sht As Worksheet, rC As RangeFor Each sht In Sheets sht.Unprotect With sht For Each rC In .UsedRange If rC.FormulaLocal <> "" Then rC.Locked = True Else rC.Locked = False End If Next End With sht.ProtectNextEnd Sub
附件可參考