我认为问题只是,如何在 VBA 中正确设置此行为。
快速使用F1提供了几乎所有必要的东西。错误在于使用Locked
,因为它必须true
适用于某些单元格,而对于所有其他单元格必须为 false 才能表现得像意图。
这是我的解决方案:
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