Code for locking all cells

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
821
Office Version
  1. 365
Platform
  1. Windows
Is there some code to protect and unprotect a sheet were it will lock all the cells? I currently have this.

ActiveSheet.Unprotect

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
After the sheet is unprotected, you need to make sure all the cells are locked:

activesheet.usedrange.locked = true

before you re-apply the protection
 
Upvote 0
ok. I should have showed you the whole Macro. My objective is to have the Macro do that for me. I have cells that can be edited, but when I run this macro I would like for it to lock all the cells for me.. Is that possible?

Sub FINAL_BURNING_MACHINES()
'
' final Macro
'
'
ActiveSheet.Unprotect
Range("J24:J26").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = 1
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ActiveCell.FormulaR1C1 = "FINAL"
Range("A1:T23").Select
ActiveSheet.PrintOut

Range("A1:T23").Select
ActiveWorkbook.Save
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True



ActiveWorkbook.Save
End Sub
 
Upvote 0
If you lock the cells then users won't be able to edit them - is that what you wanted?

If so, cleaned up code could be:

Code:
Sub FINAL_BURNING_MACHINES()
'
' final Macro
'
'
With ActiveSheet
  .Unprotect
  With .Range("J24:J26").Interior
    .Pattern = xlSolid
    .PatternColorIndex = 1
    .Color = 255
    .TintAndShade = 0
    .PatternTintAndShade = 0
  End With
  .Range("J24").FormulaR1C1 = "FINAL"
  .PrintOut
  .UsedRange.Locked = True
  .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
ActiveWorkbook.Save
End Sub
 
Upvote 0
yes, when I run the Macro I want the entire sheet locked were it is not able to be modified. Thanks
 
Upvote 0
In the code above you would just need to add a Password argument:

Rich (BB code):
.Protect Password:="SomePassword"DrawingObjects:=True, Contents:=True, Scenarios:=True
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,850
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top