Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

VBA - Excel - Locking Part of a spreadsheet

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
marksynnott
Asked:
marksynnott
  • 6
  • 6
  • 2
  • +1
1 Solution
 
Elmo_Commented:
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
 
marksynnottAuthor Commented:
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
 
Elmo_Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
marksynnottAuthor Commented:
Thanks

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

Mark
0
 
Richie_SimonettiIT OperationsCommented:
You can, but user could read it from VBA source code.
0
 
marksynnottAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
WJReidCommented:
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
 
marksynnottAuthor Commented:
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
 
WJReidCommented:
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
 
Elmo_Commented:
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
 
marksynnottAuthor Commented:
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
 
Elmo_Commented:
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
 
Elmo_Commented:
Sorry about posting twice!!
0
 
marksynnottAuthor Commented:
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
 
Elmo_Commented:
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
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

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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