vba 在某些单元格中锁定编辑但没有进一步保护
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13464141/
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
Lock editing in some cells but no further protection
提问by Daan
I'm trying to protect my Excel sheet in a way that prevents editing in certain cells but has all the other protection options disabled. Put differently, the sheet should have the behavior as an unprotected sheet only with cell editing in some cell disabled.
我正在尝试以一种防止在某些单元格中进行编辑但禁用所有其他保护选项的方式来保护我的 Excel 工作表。换句话说,只有在某些单元格中禁用单元格编辑的情况下,工作表才应具有不受保护的工作表的行为。
ActiveSheet.Range(Cells(4, 3), Cells(OldRowCount, 7)).Locked = False
ActiveSheet.Protect Contents:=True
I'd probably need to set a couple of other properties for the .Protect
, or maybe I should choose a different method altogether...
我可能需要为 设置几个其他属性.Protect
,或者我应该完全选择不同的方法......
Thanks in advance!
提前致谢!
回答by Jook
I think the question was just, how to setup this behavior in VBA properly.
我认为问题只是,如何在 VBA 中正确设置这种行为。
A quick use of F1provides nearly everything necessary. What was wrong, was the use of Locked
, because it has to be true
for some cells and false for all others in order to behave like intendet.
快速使用F1提供了几乎所有必要的东西。错误的是使用Locked
,因为它必须true
用于某些单元格而对于所有其他单元格是错误的,以便表现得像意向者。
Here's my solution:
这是我的解决方案:
Public Sub Demo()
With ActiveSheet
'Deactivate lock for all cells, so they don't be blocked by protection
.Cells.Locked = False
'Activate lock for some cells
'Range(Cells(4, 3), Cells(OldRowCount, 7)).Locked = True
.Cells(6, 4).Locked = True
If .ProtectContents Then
.Unprotect
Else
'only contents:=true is really important
.Protect _
Password:="", _
Contents:=True, _
DrawingObjects:=False, _
Scenarios:=False, _
UserInterfaceOnly:=True, _
AllowDeletingColumns:=True, _
AllowDeletingRows:=True, _
AllowFiltering:=True, _
AllowFormattingCells:=True, _
AllowFormattingColumns:=True, _
AllowFormattingRows:=True, _
AllowInsertingColumns:=True, _
AllowInsertingHyperlinks:=True, _
AllowInsertingRows:=True, _
AllowSorting:=True, _
AllowUsingPivotTables:=True
End If
End With
End Sub