[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

macro to make rows read-only in excel

Posted on 2013-12-22
9
Medium Priority
?
1,419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39734565
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 53

Expert Comment

by:Rgonzo1971
ID: 39734566
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
ID: 39734758
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:MacroShadow
ID: 39734811
First unprotect the sheet, then have the macro do its thing, then lock row and protect sheet.
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39734826
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
ID: 39735059
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 49

Accepted Solution

by:
Martin Liss earned 2000 total points
ID: 39735063
Move this to WorkBook_Open.

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

Expert Comment

by:Martin Liss
ID: 39735069
Oh, and you don't need the "Locked" line.
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39735078
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

656 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