简体   繁体   中英

How can I handle this cell-lock in excel? (VBA)

So with a macro I can create new sheets, in which some cells will be locked. I use this method in the macro:

ActiveSheet.Protect UserInterfaceOnly:=False (for the whole new sheet) Then in this macro for some cells: locked = False

Then with the locked property, other macros switch particular cells's locking using True or False. For example a table in this sheet is locked, but a button's macro recount and rewrite the values of the table, and to do this, at the beginning of the macro the table.Locked = False , and at the end of the macro Locked = True again.

But when I save the excel, close and open it again, I push the button, but I get "unable to set the Locked property"

How should I do this?

update:

Sub userinterface()

ActiveSheet.Protect UserInterfaceOnly:=True

End Sub


Sub locking()

Range("A1").Locked = False
Range("A1") = 5
Range("A1").Locked = True

End Sub

So I have a new sheet, and I run the userinterface macro. It locks the whole sheet. Then I run the locking macro, that writes 5 in the given cell and locks the cell again. After it I save the excel, close and open it, and I just want to run the locking macro. At this point I get the error.

Apparently, the UserInterfaceOnly option is not stored with the file and after reopening, the sheet is fully protected and you can't unlock cells on a protected sheet.

You can reset the UserInterfaceOnly option without unprotecting so in your example it would be

Sub locking()

    ActiveSheet.Protect UserInterfaceOnly:=True
    Range("A1").Locked = False
    Range("A1") = 5
    Range("A1").Locked = True

End Sub

Of course in this example unlocking the cell is not necessary because you have set the option to true but I assume that it's relevant in your full macro.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM