ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Protect/Unprotect Not Working (https://www.excelbanter.com/excel-programming/344664-protect-unprotect-not-working.html)

nospaminlich

Protect/Unprotect Not Working
 
I have a sheet with lots of Option Buttons and a few input cells.

The sheet is protected and each macro starts ActiveSheet.Unprotect and
finishes ActiveSheet.Protect

This is the start of the macro for Option Button 10

ActiveSheet.Unprotect
OptionButton4.Value = True (To reset to the default button in another
group)
Application.ScreenUpdating = False
Range("J4").Select
ActiveCell.FormulaR1C1 = "2"

For some reason it keeps falling over at the last line above with a run-time
error 1004 message saying the cell or chart I'm trying to change is protected
and therefore read only.

This doesn't seem to happen every time the macro runs but quite frequently.

I don't understand how this can happen given the first line in the macro
unprotects the sheet.

I'm confused. Is there a better way to write this that would be more robust?

Thanks a lot

Ken Macksey

Protect/Unprotect Not Working
 
Hi

If the code is being run from a CommandButton make sure that the
TakeFocusOnClick property of the CommandButton is set to False. Also,
although I have seen it said that it is not necessary, activate the sheet
you want and select a cell on that sheet before the unprotect statement and
your problem should disappear.

Worksheets("Sheet1").activate
Range("a1").select
ActiveSheet.Unprotect

HTH

Ken




All times are GMT +1. The time now is 01:23 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com