Need update to current VBA code to lock a particular cell & not entire sheet

rtehlan

New Member
Joined
Mar 23, 2017
Messages
19
Hi,

I am currently using this code assigned to a button which locks my sheet so no one can edit in a workbook but now i would like this code to only lock a particular cell which contains a list dropdown or range for eg. Cell C4 or C4:H4
Can someone please help me on this. Ive tried a lot but to no success. :confused:
Will be really thankful to you.

Sub LockSheet()

If ActiveSheet.ProtectContents = True Then
ActiveSheet.Unprotect
MsgBox "The Entity is now unlocked"
Else

ActiveSheet.Protect
MsgBox "The Entity is now locked"

End If


End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
This works on Excel 16 for Mac,

Code:
Sub LockSpecific()

If ActiveSheet.ProtectContents = True Then
    ActiveSheet.Unprotect
        MsgBox "Sheet Unlocked"
Else
Cells.Locked = False
[COLOR=#ff0000]    Range("C4").Locked = True 'amend to your dropdown[/COLOR]
        ActiveSheet.Protect
            ActiveSheet.EnableSelection = xlUnlockedCells
                MsgBox "Your Dropdown is locked"
End If


End Sub

If the sheet is locked, it unlocks it for you to amend as per your code,

If the sheet is unlocked for you to edit then it sets all cells protection to unlocked, less the one your drop down is in, then locks the sheet allowing the user to only select unlocked cells.
 
Upvote 0
.
Works in Excel 2007 / Win 10 as well.
 
Upvote 0
You could just unprotect the tab and then unselect the Locked option for cell C4 from the Protection tab within the Format Cells dialog. After you then reprotect the tab the users will still be able to use the drop-down but all other cells will still be protected. No code needed (if I've understood correctly).
 
Upvote 0
WOW. Works like a charm.
I only updated the msg wordings & the range reference since the drop down was in a merged cell 'C4:H4'

Thank you for your time.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,107
Members
453,021
Latest member
Justyna P

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