Link to home
Start Free TrialLog in
Avatar of marksynnott
marksynnott

asked on

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.
Avatar of Elmo_
Elmo_
Flag of Ireland image

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
Avatar of marksynnott
marksynnott

ASKER

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.
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.
Thanks

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

Mark
You can, but user could read it from VBA source code.
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.

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.
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
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
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

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
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
ASKER CERTIFIED SOLUTION
Avatar of Elmo_
Elmo_
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry about posting twice!!
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.

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.