Locking/Unlocking cells with VBA

Land_Shark918

New Member
Joined
Dec 30, 2016
Messages
13
I am trying to lock only certain cells in the worksheet. I have tried several different codes, and they work, except for the fact that, rather than locking only the 6 cells that I want locked, lets say, A1, A2, A3, A4, A5, & A6, the whole sheet ends up being locked. I have watched videos on it, and I'll do step for step the exact same thing, but yet my whole sheet continues to lock. Suggestions?
Here is the code I'm using
Sub LockDefaults()

Dim mainworkBook As Workbook

Set mainworkBook = ActiveWorkbook

mainworkBook.Sheets("Initial Input").Range("A1:A6").Value = "Locked"

mainworkBook.Sheets("Main").Range("A1:C5").Locked = True

ActiveSheet.Protect Password:="12345"


End Sub
 

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
Hi:

I believe the default is that ALL Cells are locked on a new spreadsheet. SO If you want the entire sheet to be Unprotected the first thing you need to do is select the ENTIRE sheet, Right click and select Format Cells and then go to Protection and remove the check mark from Locked. Now when you protect the sheet no cells are protected and you can likely run your code to lock specific cells. Hope this makes sense :)

Good Luck :)
 
Upvote 0

Forum statistics

Threads
1,216,503
Messages
6,131,020
Members
449,615
Latest member
Nic0la

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