Locking(Protecting) Unused Cells

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hey guys, how to lock(protect) unused cells in a sheet using macro.Sheet data will be dynamic.Thank you.
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
What is the layout of your sheet?
Do you a "block" of data, where your want everything to the right of it, and everything underneath it locked?
Is it that straightforward?
Or might you have mixed blanks cells interspersed with your data that you also want locked?
 
Upvote 0
My data always will be in C to I column but the rows are dynamic. I don't want others to add additional data in the sheet. So i want to lock the unused cells. If its fixed range of data then it will be easy to lock. But here the data is dynamic so i have no clue how to do it. I do have blank cells in between, but that doesn't matter. I want to lock after the last row used and the cells around C and I column.
 
Upvote 0
So, tell me if this sounds right.
- We can dynamically find the last row of data by looking in column C
- Once we find that, we want to leave columns C-I from rows 1 down to the last row unlocked, and lock EVERYTHING else.

Is that correct?
If so, this code should do what you want:
Code:
Sub MyProtectMacro()

    Dim lRow As Long
    
'   Use column C to find last row
    lRow = Cells(Rows.Count, "C").End(xlUp).Row
    
'   Unprotect columns C-I down to last row
    Range("C1:I" & lRow).Locked = False
    
'   Protect sheet with password
    ActiveSheet.Protect Password:="pass"

End Sub
 
Upvote 0
Thanks Joe, it working great. but one of the sheet is not locking any unused cells i don't know what is the reason. Do you have any idea on this?
 
Upvote 0
On any new blank sheet, all cells are "locked" to begin, by default. But if someone has already unlocked them all, you will need to lock them all to start.
We can add that to the code, like this:
Code:
Sub MyProtectMacro()

    Dim lRow As Long
    
[COLOR=#ff0000]'   Make sure all cells are locked to start
    Cells.Locked = True[/COLOR]
    
'   Use column C to find last row
    lRow = Cells(Rows.Count, "C").End(xlUp).Row
    
'   Unprotect columns C-I down to last row
    Range("C1:I" & lRow).Locked = False
    
'   Protect sheet with password
    ActiveSheet.Protect Password:="pass"

End Sub
 
Last edited:
Upvote 0
All the cells are locked only. The cells after I columns are locked. The cells below the last used row and column A and B are not locked.
 
Last edited:
Upvote 0
All the cells are locked only. The cells after I columns are locked. The cells below the last used row and column A and B are not locked.
I don't see how that is possible.
The code begins of locking ALL cells in on the sheet.
Then it only unlocks certains rows in column C-I. It doesn't touching columns A or B for the unlocking.

You don't have any merged cells, do you?
Or any other VBA code running?
 
Upvote 0
Hi Joe, now its working after closing and open the WB again. i don't know how it got fixed. Thanks for your efforts....
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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