vba 选择特定的锁定单元格,让其他单元格“不可选择”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19478559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:55:49  来源:igfitidea点击:

Select specific locked cells, leaving other cells 'unselectable'

excelexcel-vbaprotectionvba

提问by ExcelForFreelancers

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

采纳答案by varocarbas

When you protect a worksheet, the behaviour in each cell is defined by the Lockedproperty. 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 A1but not cell A2. Bear in mind that, by default, the Lockedproperty is set to True.

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

CLARIFICATION

澄清

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

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