I tried creating an Add-In to store my code.
Now my code doesn't seem to work.
My code loops through each sheet and therefore was being stored in "ThisWorkbook" and is triggered on the Open & Save events for the file.
It wasn't being stored in a separate module.
It has to work for a variety of different files for different users, so I want it to remain pretty general.
Any suggestions?
Thanks,
Eliz
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'reduce screen flicker
Application.ScreenUpdating = False
'declare variables
Dim SH As Worksheet
Dim rng As Range
On Error Resume Next
'loop through each sheet, lock formulas, protect sheets
For Each SH In Worksheets
SH.Unprotect
With SH.UsedRange
.Locked = False
Set rng = Nothing
Set rng = .SpecialCells(xlCellTypeFormulas)
If Not rng Is Nothing Then
'rng.Font.ColorIndex = 7 'optional changes color of all formulas
rng.Locked = True
End If
End With
' allow users to still insert rows with protection on
SH.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowInsertingColumns:=True, AllowInsertingRows:=True
Next SH
Application.ScreenUpdating = True
End Sub
Private Sub Workbook_Open()
'reduce screen flicker
Application.ScreenUpdating = False
Application.CommandBars("Protection").Visible = True
'declare variables
Dim SH As Worksheet
Dim rng As Range
On Error Resume Next
'loop through each sheet, lock formulas, protect sheets
For Each SH In Worksheets
SH.Unprotect
With SH.UsedRange
.Locked = False
Set rng = Nothing
Set rng = .SpecialCells(xlCellTypeFormulas)
If Not rng Is Nothing Then
'rng.Font.ColorIndex = 7 'optional changes color of all formulas
rng.Locked = True
End If
End With
' allow users to still insert rows with protection on
SH.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowInsertingColumns:=True, AllowInsertingRows:=True
Next SH
Application.ScreenUpdating = True
End Sub
Now my code doesn't seem to work.
My code loops through each sheet and therefore was being stored in "ThisWorkbook" and is triggered on the Open & Save events for the file.
It wasn't being stored in a separate module.
It has to work for a variety of different files for different users, so I want it to remain pretty general.
Any suggestions?
Thanks,
Eliz
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'reduce screen flicker
Application.ScreenUpdating = False
'declare variables
Dim SH As Worksheet
Dim rng As Range
On Error Resume Next
'loop through each sheet, lock formulas, protect sheets
For Each SH In Worksheets
SH.Unprotect
With SH.UsedRange
.Locked = False
Set rng = Nothing
Set rng = .SpecialCells(xlCellTypeFormulas)
If Not rng Is Nothing Then
'rng.Font.ColorIndex = 7 'optional changes color of all formulas
rng.Locked = True
End If
End With
' allow users to still insert rows with protection on
SH.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowInsertingColumns:=True, AllowInsertingRows:=True
Next SH
Application.ScreenUpdating = True
End Sub
Private Sub Workbook_Open()
'reduce screen flicker
Application.ScreenUpdating = False
Application.CommandBars("Protection").Visible = True
'declare variables
Dim SH As Worksheet
Dim rng As Range
On Error Resume Next
'loop through each sheet, lock formulas, protect sheets
For Each SH In Worksheets
SH.Unprotect
With SH.UsedRange
.Locked = False
Set rng = Nothing
Set rng = .SpecialCells(xlCellTypeFormulas)
If Not rng Is Nothing Then
'rng.Font.ColorIndex = 7 'optional changes color of all formulas
rng.Locked = True
End If
End With
' allow users to still insert rows with protection on
SH.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowInsertingColumns:=True, AllowInsertingRows:=True
Next SH
Application.ScreenUpdating = True
End Sub