macro to make rows read-only in excel


How can I make a row read-only in a sheet via a macro?

kindly share your code
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joe HowardCommented:
This will lock the first row, to lock a different row just change the row number of the ranges.

Sub Demo()
    Range("A1:XFD1").Locked = True
    ActiveSheet.Protect Contents:=True
End Sub

Open in new window

Note: locking cells only applies when the sheet is protected, hence the second line of code.

pls try
Sub Macro5()

    Cells.Locked = False
    Range("1:1").Locked = True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub

Open in new window

EDIT By default cells are locked, if you protect without unlocking the other cells, you will lock all of them

rutgermonsAuthor Commented:

thanks! can I though ensure the rows are locked but manual user input but not if the same macro wants to update the rows?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Joe HowardCommented:
First unprotect the sheet, then have the macro do its thing, then lock row and protect sheet.
Martin LissOlder than dirtCommented:
Instead of locking and unlocking and taking the (very small) chance that the user will change the sheet before it's locked again, just do this instead of locking.

ActiveSheet.Protect UserInterfaceOnly:=True

If that's done the sheet will be locked but code can run against it.
rutgermonsAuthor Commented:

I used your code in

 Sheets("XI End Points").Cells(c6, 1).Value = Me.ComboBox1.Value
    Sheets("XI End Points").Cells(c6, 2).Value = Me.ComboBox1.Column(4)
    Sheets("XI End Points").Cells(c6, 3).Value = Me.ComboBox1.Column(5)
    Sheets("XI End Points").Cells(c6, 4).Value = Me.ComboBox1.Column(6)
    Range("A3:XFD1").Locked = True
    Sheets("XI End Points").Protect UserInterfaceOnly:=True

no luck

any ideas?
Martin LissOlder than dirtCommented:
Move this to WorkBook_Open.

Sheets("XI End Points").Protect UserInterfaceOnly:=True

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
Oh, and you don't need the "Locked" line.
Martin LissOlder than dirtCommented:
One more thing. You probably know this but just in case...

After you move that line to Workbook_Open you either have to close and reopen the workbook or run that sub from Visual Basic.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.