Solved

macro to make rows read-only in excel

Posted on 2013-12-22
9
1,136 Views
Last Modified: 2013-12-28
folks

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

kindly share your code
0
Comment
Question by:rutgermons
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
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
 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
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
 

Author Comment

by:rutgermons
Comment Utility
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
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
First unprotect the sheet, then have the macro do its thing, then lock row and protect sheet.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
 

Author Comment

by:rutgermons
Comment Utility
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
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
Comment Utility
Move this to WorkBook_Open.

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

Expert Comment

by:Martin Liss
Comment Utility
Oh, and you don't need the "Locked" line.
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now