I have a large workbook with numerous sheets. The sheets contain a lot of formulas. Is…
- User
I have a large workbook with numerous sheets. The sheets contain a lot of formulas. Is there a VBA code that can prevent all the cells that contain formulas from being edited, changed or deleted.
- Assistant
Yes, you can use VBA code to protect the cells that contain formulas from being edited, changed, or deleted. Here is an example code that you can use:
Sub ProtectFormulas() Dim ws As Worksheet Dim rng As Range ' Loop through all worksheets in the workbook For Each ws In ThisWorkbook.Worksheets ' Define the range to protect as all cells with formulas Set rng = ws.Cells.SpecialCells(xlCellTypeFormulas) ' Check if there are any cells with formulas in the worksheet If Not rng Is Nothing Then ' Protect the range of cells with formulas rng.Locked = True rng.FormulaHidden = True ' Protect the worksheet with a password ws.Protect Password:="YourPassword", UserInterfaceOnly:=True End If Next ws End SubTo use this code, follow these steps:
- Press
Alt + F11to open the VBA Editor in Excel.
- Press
- User
If I use this code and it does not work to my requirements, how then can I unlock all the cells it has locked
- Assistant
If you want to unlock all the cells that were previously locked using the code provided, you can use the following VBA code to remove the protection from all worksheets in the workbook:
Sub UnprotectAllSheets() Dim ws As Worksheet ' Loop through all worksheets in the workbook For Each ws In ThisWorkbook.Worksheets ' Unprotect the worksheet with the password ws.Unprotect Password:="YourPassword" ' Unlock all cells in the worksheet ws.Cells.Locked = False Next ws End SubTo use this code, follow these steps:
- Press
Alt + F11to open the VBA Editor in Excel. - Insert a new module by clicking
Insert -> Module. - Copy and paste the above code into the module.
- Replace
"YourPassword"in the code with the password that was used to protect the worksheets. - Close the VBA Editor.
- Run the code by pressing
Alt + F8, selectingUnprotectAllSheets, and clickingRun.
This code will remove the protection from all worksheets in the workbook, allowing you to edit, change, or delete any previously locked cells.
- Press
- User
What about the following, dont they need to be set to flase: rng.Locked = True rng.FormulaHidden = True
- Assistant
Yes, you are correct. In order to unlock the cells, you need to set the
LockedandFormulaHiddenproperties toFalse. Here's an updated version of the code to unlock all the previously locked cells:Sub UnprotectAllSheets() Dim ws As Worksheet Dim rng As Range ' Loop through all worksheets in the workbook For Each ws In ThisWorkbook.Worksheets ' Unprotect the worksheet with the password ws.Unprotect Password:="YourPassword" ' Check if there are any locked cells in the worksheet On Error Resume Next Set rng = ws.Cells.SpecialCells(xlCellTypeFormulas, 23) On Error GoTo 0 ' Unlock the locked cells and unhide the formulas If Not rng Is Nothing Then rng.Locked = False rng.FormulaHidden = False End If ' Protect the worksheet without a password ws.Protect UserInterfaceOnly:=True Next ws End SubMake sure to replace
"YourPassword"in the code with the password that was used to protect the worksheets. This updated code will unprotect the worksheet, unlock the previously locked cells, unhide the formulas, and then protect the worksheet again without a password, allowing users to interact with all cells. - User
The vba code below sets rng.Locked to True, It also sets FoormulaHidden to True. It then password protects the sheet and sets UserInterfaceOnly to True for all sheets within a workbook. Please write me another vba code that undoes all the changes and returns the workbook and all its worksheets to their previous state before the code was run: Sub ProtectFormulas() Dim ws As Worksheet Dim rng As Range For Each ws In ThisWorkbook.Worksheets Set rng = ws.Cells.SpecialCells(xlCellTypeFormulas) If Not rng Is Nothing Then rng.Locked = True rng.FormulaHidden = True ws.Protect Password:="edit", UserInterfaceOnly:=True End If Next ws End Sub
- Assistant
To undo the changes made by the provided VBA code and return the workbook and all its worksheets to their previous state, you can use the following VBA code:
Sub UndoProtection() Dim ws As Worksheet Dim rng As Range ' Disable error handling On Error Resume Next ' Loop through all worksheets in the workbook For Each ws In ThisWorkbook.Worksheets ' Unprotect the worksheet with the password ws.Unprotect Password:="edit" ' Check if there are any locked cells in the worksheet Set rng = ws.Cells.SpecialCells(xlCellTypeFormulas, 23) ' Unlock the previously locked cells and unhide the formulas If Not rng Is Nothing Then rng.Locked = False rng.FormulaHidden = False End If ' Protect the worksheet without a password ws.Protect UserInterfaceOnly:=False Next ws ' Enable error handling On Error GoTo 0 End SubMake sure to replace
"edit"in the code with the password that was used to protect the worksheets. This code will unprotect the worksheets, unlock the previously locked cells, unhide the formulas, and protect the worksheets without a password, restoring the workbook and all its worksheets to their previous state.