VBA Only Protect Populated Cells

kaylan

New Member
Joined
Aug 19, 2019
Messages
9
Hello,

I need to create a button that allows a manager to lock all data input, but will then allow other users to add to the sheet below what has already been locked. I know how to set up the button and password, but everything I do to protect the sheet ends up protecting everything -- including the empty cells. My understanding thus far is that the best solution is to code something that unlocks the empty cells.

The closest I have gotten is the code below (which I got from another site). The problem is that this formula is only unlocks empty cells if they are between populated rows. My sheet needs to have an unlimited number of rows. Simply adding something to the very last row of the sheet does work, but then the macro is slow to run -- not ideal. I know there has to be a better way to do this.

I plan on adding the code for the password once I get the locking/unlocking portion functional:

Sub UnlockEmptyCells()
Dim myCell As Range


ActiveSheet.Unprotect


Set myCell = Selection
Cells.Select
Selection.Locked = True
myCell.Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Locked = False


ActiveSheet.Protect DrawingObjects:=True, _
Contents:=True, Scenarios:=True
myCell.Select
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try it backwards: first unlocking all cells and then locking just the ones with values.

Code:
Sub UnlockEmptyCells()
    Dim myCell As Range
    
    Set myCell = Selection
    Cells.Select
    Selection.Locked = False
    myCell.Select
    Selection.SpecialCells(xlCellTypeConstants).Select
    Selection.Locked = True
    myCell.Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
 
Upvote 0
Thank you for the suggestion!!

When I try the code suggested, I get Run-time error '1004': Unable to set the Locked Property of the Range class.

I'm going to play around with your code and see if I can get it to work, but if you have any other thoughts please let me know.
 
Upvote 0
Actually scratch that -- my first solution tried fixed it! I simply needed to unprotect the sheet first, then it works. Thank you so much for your help!
 
Upvote 0
Sorry about that. My fault for not putting in the Unprotect line. Glad it worked in the end!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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