Solved

VBA - Excel - Locking Part of a spreadsheet

Posted on 2002-06-24
16
392 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
  • 6
  • 6
  • 2
  • +1
16 Comments
 
LVL 3

Expert Comment

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

Author Comment

by:marksynnott
Comment Utility
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
Comment Utility
You can, but user could read it from VBA source code.
0
 

Author Comment

by:marksynnott
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:marksynnott
Comment Utility
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
Comment Utility
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_
Comment Utility
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
Comment Utility
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
Comment Utility
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_
Comment Utility
Sorry about posting twice!!
0
 

Author Comment

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 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

6 Experts available now in Live!

Get 1:1 Help Now