• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1681
  • Last Modified:

macro to make rows read-only in excel

folks

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

kindly share your code
0
rutgermons
Asked:
rutgermons
  • 4
  • 2
  • 2
  • +1
1 Solution
 
MacroShadowCommented:
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.
0
 
Rgonzo1971Commented:
Hi,

pls try
Sub Macro5()

    ActiveSheet.Unprotect
    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

Regards
0
 
rutgermonsAuthor Commented:
folks

thanks! can I though ensure the rows are locked but manual user input but not if the same macro wants to update the rows?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
MacroShadowCommented:
First unprotect the sheet, then have the macro do its thing, then lock row and protect sheet.
0
 
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.
0
 
rutgermonsAuthor Commented:
Martin

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?
0
 
Martin LissOlder than dirtCommented:
Move this to WorkBook_Open.

Sheets("XI End Points").Protect UserInterfaceOnly:=True
0
 
Martin LissOlder than dirtCommented:
Oh, and you don't need the "Locked" line.
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now