简体   繁体   English

如何在Excel中处理此单元锁? (VBA)

[英]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 ActiveSheet.Protect UserInterfaceOnly:=False (对于整个新工作表)然后在此宏中的某些单元格: locked = False

Then with the locked property, other macros switch particular cells's locking using True or False. 然后使用locked属性,其他宏使用True或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. 例如,此工作表中的一个表被锁定,但是按钮的宏重新计数并重写该表的值,并为此在宏的table.Locked = False和宏的末尾Locked = True一次。

But when I save the excel, close and open it again, I push the button, but I get "unable to set the Locked property" 但是当我保存excel时,关闭并再次打开它时,我按下了按钮,但是却收到"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. 因此,我有了一个新的工作表,并运行了userinterface宏。 It locks the whole sheet. 它锁定整个工作表。 Then I run the locking macro, that writes 5 in the given cell and locks the cell again. 然后,我运行锁定宏,该宏在给定的单元格中写入5并再次锁定该单元格。 After it I save the excel, close and open it, and I just want to run the locking macro. 之后,我保存了excel,将其关闭并打开,我只想运行锁宏。 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. 显然, UserInterfaceOnly选项未与文件一起存储,并且在重新打开后,工作表已受到完全保护,并且您无法解锁受保护工作表上的单元格。

You can reset the UserInterfaceOnly option without unprotecting so in your example it would be 您可以在不取消保护的情况下重置UserInterfaceOnly选项,因此在您的示例中

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. 当然,在此示例中,解锁单元不是必需的,因为您已将选项设置为true,但我认为它与完整宏相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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