Lock and unlock cell with macro

partsbbw

New Member
Joined
Jan 18, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I am looking for a code to apply to a button that will lock a row and leave all other cells/rows below it to remain editable. I will likely have data in $A1:$H1 and once the data is entered, a button is clicked to lock that row. And then the next row will have data entered and click a button to lock that row and so on. Multiple users will use this sheet to enter data. I just want to prevent an error in changing data in a row already entered.
If this is possible would be great.
Another step, although not necessary, but if another button could be pressed to unlock a row if it does need to be edited. The row will not have to be password protected.
Thank you
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Welcome to the Board!

If I understand your needs, the following should work.

First, we need to do some preliminary work:
1. Unlock ALL cells on your worksheet by selectinh all cells in your workbook, and go to "Format Cells", then go to the "Protection" tab and unselect the "Locked" box
2. Go to the Review menu, select "Protect Sheet", and protect it with a password of your choosing.

Now, paste the following VBA code in your VB Editor:
VBA Code:
Sub MyLockRow()

'   First unprotect sheet
    ActiveSheet.Unprotect Password:="password"
    
'   Lock current row
    Rows(ActiveCell.Row).Locked = True

'   First unprotect sheet
    ActiveSheet.Protect Password:="password"
    
End Sub
This code will protect whatever the active row is, and change nothing else.
You can attach this code to a button or keyboard shortcut, if you like.
 
Upvote 0
Solution
You are welcome!
Glad I was able to help.
 
Upvote 0

Forum statistics

Threads
1,224,803
Messages
6,181,055
Members
453,014
Latest member
Chris258

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