Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Protect and Unprotect in VBA 1

Status
Not open for further replies.

anet

Programmer
Jul 10, 2001
35
CA
I want to stop a user from entering information on a worksheet that is being updated through VBA code. I am using ActiveSheet.Unprotect, updating the cells, then using ActiveSheet.Protect at the end of the sub. The problem is that when the sheet is unprotected, the updated cells are somehow being reverted to "unlocked" so that when the Protect event runs the only cells that are protected are those that were not changed in my code.

How can I stop the cell format from changing and why is this happening?

Thanks for any help!
 
How are you assigning values to the cells in question ?. Are you using paste. If so, use PasteSpecial :

e.g.

Range("A1").PasteSpecial Paste:=xlValues

You could also use

Range("A1").Locked = True, after you change the value.

A.C
 
Thanks. I was actually using:

Cells(MyRow, 1).Copy Sheets("Pay Slip").Range("G1")

to copy data from one worksheet to another.

So, I added a loop at the end of my sub as follows:

For Each rCell In Selection
If rCell.Locked = False Then
rCell.Locked = True
End If
Next rCell
Sheets("Pay Slip").Protect

Now it seems to be working fine.
A gold star for you! [2thumbsup]
 
I know you've solved this but as another way of looking at the issue, how about this:
ActiveSheet.Protect contents:=True, userinterfaceonly:=True

This will allow you to change the sheet via code but users cannot change it manually - could save a LOT of .protect / .unprotect statements... Rgds
~Geoff~
 
Thanks Geoff. I tried that first but couldn't get it to work. Maybe I was putting the code in the wrong place? I had placed it in the Worksheet Activate event, is there a better place?

Annette
 
OK - this is getting really frustrating. The code that was working yesterday (if rCell.locked = false then rCell.locked = true) today is not and I didn't change anything! Now I am getting a message that says "unable to set the Locked property of the Range class." I tried leaving out the if statement and just put the following:

Set rRange = Sheets("Pay Slip").Range("A1:G19")
For Each rCell In rRange
rCell.Locked = True
Next rCell

Surprise, surprise, it didn't work either.

So I commented out that whole section and tried putting the following in my Workbook Open event:

Sheets("Pay Slip").Protect contents:=True, userinterfaceonly:=True

This protects the worksheet, but will not allow updates even in code.

Does anyone have any ideas?

Annette

PS - I am using Excel 2000
 
Hi,
try this i am using it in a spreadsheet which only allow user input through forms i have created. and we have no problem at all


Sheets(&quot;Pay Slip&quot;).Protect (&quot;<Use your own password>&quot;), UserInterfaceOnly:=True

This is set at workbook open and again at workbook close just in case someone has unprotected the sheet for some maintenance on it

Regards
Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top