excelinter
New Member
- Joined
- Feb 3, 2010
- Messages
- 10
Hi,
I have a excel workbook with several worksheets and I want to be able to protect all the formulas in the workbook, but allow row/column insertion, deletion, formatting of cells and expanding/collapsing groups of rows. I have several workbooks on which I need to perform this action. So, I got the macro below for performing this action - the macro protects all the cells which have formulas, but then it does not allow me to format rows/columns/cells or insert rows/columns even though I have explicity set those parameters to True. I am using Excel 2007. Can some take a look and let me know what I'm missing. Any help is greatly appreciated !
Sub protect_formulas()
Dim wks As Worksheet
Dim wkbk As Workbook
Dim cl As Range
Set wkbk = ActiveWorkbook
For Each wks In wkbk.Worksheets
wks.Unprotect Password:="budget"
wks.Cells.Locked = False
For Each cl In wks.UsedRange
If cl.HasFormula Then
cl.Locked = True
End If
Next cl
wks.Protect Password:="budget"
wks.Protect AllowFormattingColumns:=True
wks.Protect AllowInsertingColumns:=True
wks.Protect AllowInsertingRows:=True
wks.Protect AllowFormattingRows:=True
wks.Protect AllowFormattingCells:=True
Next wks
End Sub
I have a excel workbook with several worksheets and I want to be able to protect all the formulas in the workbook, but allow row/column insertion, deletion, formatting of cells and expanding/collapsing groups of rows. I have several workbooks on which I need to perform this action. So, I got the macro below for performing this action - the macro protects all the cells which have formulas, but then it does not allow me to format rows/columns/cells or insert rows/columns even though I have explicity set those parameters to True. I am using Excel 2007. Can some take a look and let me know what I'm missing. Any help is greatly appreciated !
Sub protect_formulas()
Dim wks As Worksheet
Dim wkbk As Workbook
Dim cl As Range
Set wkbk = ActiveWorkbook
For Each wks In wkbk.Worksheets
wks.Unprotect Password:="budget"
wks.Cells.Locked = False
For Each cl In wks.UsedRange
If cl.HasFormula Then
cl.Locked = True
End If
Next cl
wks.Protect Password:="budget"
wks.Protect AllowFormattingColumns:=True
wks.Protect AllowInsertingColumns:=True
wks.Protect AllowInsertingRows:=True
wks.Protect AllowFormattingRows:=True
wks.Protect AllowFormattingCells:=True
Next wks
End Sub