选择特定的锁定单元格,让其他单元格无法选择

时间:2021-11-18 02:30:57

In Excel VBA, is there a way to select specific locked cells, while leaving other cells 'unselectable'?

在Excel VBA中,有没有办法选择特定的锁定单元格,同时保留其他单元格的“不可选择”?

I have an excel sheet, which is protected. I would like to give the user the ability to select specific locked cells within a sheet while at the same time not allowing them to select other locked cells within the same sheet. Basically I have a small, cell-based calendar, in which I would like them to be able to select specific dates (locked cells) on this small range, however there is no need for them to select any other locked cells within the sheet. Excels Sheet Protection only seems to allow a sheet-wide "Select Lock Cells". If there is a way to accomplish this through VBA I would love to hear about it. I did look over a lot of other posts before posting here.

我有一张excel表,受到保护。我想让用户能够在工作表中选择特定的锁定单元格,同时不允许他们在同一工作表中选择其他锁定单元格。基本上我有一个小的,基于单元格的日历,我希望他们能够在这个小范围内选择特定日期(锁定单元格),但是他们不需要在工作表中选择任何其他锁定单元格。 Excel表格保护似乎只允许在表格范围内“选择锁定单元格”。如果有办法通过VBA实现这一点,我很乐意听到它。在发布之前我确实查看了很多其他帖子。

1 个解决方案

#1


2  

When you protect a worksheet, the behaviour in each cell is defined by the Locked property. Sample code:

保护工作表时,每个单元格中的行为由Locked属性定义。示例代码:

ActiveSheet.Unprotect
ActiveSheet.Range("A1").Locked = False
ActiveSheet.Range("A2").Locked = True
ActiveSheet.Protect
ActiveSheet.EnableSelection = xlNoRestrictions 'All the cells can be selected 

With this code, users can edit cell A1 but not cell A2. Bear in mind that, by default, the Locked property is set to True.

使用此代码,用户可以编辑单元格A1,但不能编辑单元格A2。请记住,默认情况下,Locked属性设置为True。

CLARIFICATION

澄清

Protection is applied to the whole worksheet. At the cell level, all what you can do is relying on the Locked property. What you cannot get is different behaviours for different (Locked/Unlocked) ranges.

保护适用于整个工作表。在单元级别,您可以做的就是依赖Locked属性。你不能得到的是不同(锁定/解锁)范围的不同行为。

#1


2  

When you protect a worksheet, the behaviour in each cell is defined by the Locked property. Sample code:

保护工作表时,每个单元格中的行为由Locked属性定义。示例代码:

ActiveSheet.Unprotect
ActiveSheet.Range("A1").Locked = False
ActiveSheet.Range("A2").Locked = True
ActiveSheet.Protect
ActiveSheet.EnableSelection = xlNoRestrictions 'All the cells can be selected 

With this code, users can edit cell A1 but not cell A2. Bear in mind that, by default, the Locked property is set to True.

使用此代码,用户可以编辑单元格A1,但不能编辑单元格A2。请记住,默认情况下,Locked属性设置为True。

CLARIFICATION

澄清

Protection is applied to the whole worksheet. At the cell level, all what you can do is relying on the Locked property. What you cannot get is different behaviours for different (Locked/Unlocked) ranges.

保护适用于整个工作表。在单元级别,您可以做的就是依赖Locked属性。你不能得到的是不同(锁定/解锁)范围的不同行为。