Solved

VBA - Excel - Locking Part of a spreadsheet

Posted on 2002-06-24
16
404 Views
Last Modified: 2010-05-02
Hello

Is there a way to lock part of an excewl spreadsheet from users amending that part.

for example I want to lock cells a1:t1200 but allow users to amend all other cells in the sheet is there a way to do this ????

Deletion etc of relevant cells shpould be prohibited.
0
Comment
Question by:marksynnott
[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
  • 6
  • 6
  • 2
  • +1
16 Comments
 
LVL 3

Expert Comment

by:Elmo_
ID: 7104200
Mark,

You have already posted this question.  If you hit the Refresh button It will keep posting the question.  Use the Reload Question option instead.

This comment is also posted on your previous post of this question.

ed.

--------------------------------


This should allow you to do this.

By Default all cells are Locked so, If you select all cells and change the locked property to false and then select the cells you would like to protect and change their locked property back to True.  You can not protect your worksheet.

For this just go to tools/protection/protect sheet

Here is the macro you will need.

Cheers,

Ed.

Sub Protect1()
   Cells.Select
   Selection.Locked = False
   Selection.FormulaHidden = False
   Range("A1").Select
   Application.Goto Reference:="R1C1:R1200C20"
   Selection.Locked = True
   Selection.FormulaHidden = False
   ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
0
 

Author Comment

by:marksynnott
ID: 7104213
Thanks Wlmo,

On the 2nd line i gen an error

"Unable to lock property of the range class"

Sorryu I have hit the refresh in error a few times.
0
 
LVL 3

Expert Comment

by:Elmo_
ID: 7104236
Mark,

Did you enter this macro into a Module in the Visual Basic Editor for Excel?

(Open a Fresh excel Workbook. Press alt+F11.  Goto Insert/Module.  Paste the code.  Press Play)

The way I generated this code is, I recorded a macro.  I usually do this and then modify the code to suit my needs.

Its best to test code like this on a fresh sheet.

Cheers,

Ed.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:marksynnott
ID: 7104439
Thanks

I got it working. Just one more thing, anyone can unprotect it, can i add a password through VBA ???

Mark
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7104662
You can, but user could read it from VBA source code.
0
 

Author Comment

by:marksynnott
ID: 7105078
Doeas know how to eneter the password through vba

Well they wont be able to dsee the protection password as the vba code is protected by a anoither password.

Also when i send the sheet on it wont contain the vba as its an add in.

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7105153
In VBA IDE:
go to tolls, VBA project properties.
Go to  Protection (no so strong ;) tab and there you could set the password for your project.
0
 
LVL 13

Expert Comment

by:WJReid
ID: 7105695
Hi marksynnott,

You can protect the cells without code by the following:

cover the range A1:T1200 and select data validation. From the allow box select custom and the formula box will appear. In the formula box type: =""

Click the Input Message  tab and type in a message such as "This cell is protected".

Click on the Error Alert tab and put in a message such as "You cannot enter data in this cell". Users will seee this message if they try to type anything in the cell. Set the error level from the style box, then click OK.
If you try to type anything in the cells, you will receive the error message above.

Regards,

WJReid
0
 

Author Comment

by:marksynnott
ID: 7106814
Hi All

I think there is a bit of confusion here.

I need to protect the worksheet through vba. When i do it through the menus i have an option to enter a password.


Richie
I am well aware how to lock a VBA project but that is completely different from protecting a spreadsheet
0
 
LVL 13

Expert Comment

by:WJReid
ID: 7106830
Hi marksynnott,

You do not protect the sheet through the menus with the method I have given above. You do it by Data|Validation, selecting custom and then typing in =""

You will not be asked if you want to enter a password. You can put whatever messages you like when users select the cell and a different message when they try to enter data into it.

Regards,

WJReid

0
 
LVL 3

Expert Comment

by:Elmo_
ID: 7106902
Mark,

This should Work now.

It will do as before but this time it will allow you to add a password.

If you have any more problems, just post.

Cheers,

Ed.

---------------------

Sub Protect1()
  Cells.Select
  Selection.Locked = False
  Selection.FormulaHidden = False
  Range("A1").Select
  Application.Goto Reference:="R1C1:R1200C20"
  Selection.Locked = True
  Selection.FormulaHidden = False
  ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="ED"
End Sub
0
 

Author Comment

by:marksynnott
ID: 7106903
Thanks WJ

For your help.

However the problem with that soloution is that any user can go in and remove the data validation. Also everything must be done ythrough VBA as the user clicks on a button.

Does anyone know how i look through the VBA object model to try ands see how i put a password in ??????


Thanks again
0
 
LVL 3

Accepted Solution

by:
Elmo_ earned 100 total points
ID: 7106905
Mark,

This should Work now.

It will do as before but this time it will allow you to add a password.

If you have any more problems, just post.

Cheers,

Ed.

---------------------

Sub Protect1()
 Cells.Select
 Selection.Locked = False
 Selection.FormulaHidden = False
 Range("A1").Select
 Application.Goto Reference:="R1C1:R1200C20"
 Selection.Locked = True
 Selection.FormulaHidden = False
 ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="ED"
End Sub
0
 
LVL 3

Expert Comment

by:Elmo_
ID: 7106907
Sorry about posting twice!!
0
 

Author Comment

by:marksynnott
ID: 7110158
Thanks a million, that works brilliantly.

Just a quick question

Whatr is the difference between

password = "ww"

password:= "ww"

as i tried the = on its own and it didnt work.

0
 
LVL 3

Expert Comment

by:Elmo_
ID: 7110176
As far as I can remember off hand the difference is that with:

password = "ww" - You are assigining a value to this "varaible"

password:= "ww" - You are setting a parameter which is part of an Excel command.

Hope this helps.

Ed.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

738 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