PDA

View Full Version : Protect a Worksheet except while macro is running



jungix
09-07-2006, 12:11 PM
Hi,

I would like to create a worksheet with some unlocked cells for the user to put inputs, and then launch a macro that writes the outputs in some other cells. I would like to lock the cells containing the outputs. How can I unprotect the sheet in my macro, and protect it again afterwards? Note that the user won't have the password to unlock the cells.

I hope I was clear enough.

Jungix

mvidas
09-07-2006, 12:22 PM
Jungix,

While you could protect the sheet on macro start and unprotect when it is complete, you could also just protect it setting the UserInterfactOnly argument to true. That keeps it unprotected for macros (while the workbook is open, it needs to be set again when closed/reopened) but protected to the user.
Sheets("Name").Protect Password:="abc",UserInterfaceOnly:=True
Look in the VBA help at the protect method for more details.
Matt

mdmackillop
09-07-2006, 12:25 PM
.. but here's the unprotect version anyway!

ActiveSheet.Unprotect Password:="test"
ActiveCell.FormulaR1C1 = "Testing"
ActiveSheet.Protect Password:="test", DrawingObjects:=True, Contents:=True, Scenarios:=True

jungix
09-07-2006, 12:48 PM
Thanks!

I didn't think of putting the password in my code itself. What a dumb I am!