0

我想允许我的文档的用户编辑我的工作表上的特定形状,即 Shape.Type = msoOvalShapes

但是,当我使用 DrawingObjects:=False 保护我的工作表时,可以编辑所有对象,包括例如我希望保持静止和不受影响的图片和矩形。

我已经进行了一些研究,但还没有找到将绘图对象限制为仅特定对象类型的示例。以下是我当前的 Workbook_Open 协议。

Private Sub Workbook_Open()

Application.ScreenUpdating = False
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")

ws.Activate
ActiveWindow.Zoom = 100

With ws
   .Protect Password:="", UserInterfaceOnly:=True
   .EnableOutlining = True
   .Protect DrawingObjects:=False, Contents:=True, Scenarios:=True
End With
Application.ScreenUpdating = True

End Sub

是否可以保护工作表中的特定对象?

4

1 回答 1

1

当您执行默认的“.Protect”时,例如在 ThisWorbook > Sub Workbook_Open 中,通过 Excel 应用程序工作表中的“属性”选项卡锁定的所有项目都无法移动或编辑。

但是,当通过命令按钮添加新对象/形状时,它们也会被锁定。

为了更改它以使新添加的对象/形状不被锁定,我进入创建对象的 Sub 并将通过此 Sub 添加的任何新对象的“锁定”属性设置为 False。例如,这是我的代码:

Sub AddShape()
Dim s As Shape
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")

Set s = ws.Shapes.AddShape(msoShapeOval, 775 + w * 3, 100 + w * 2, 20, 20)
s.TextFrame.Characters.Text = w
s.TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
s.TextFrame2.VerticalAnchor = msoAnchorMiddle
s.Fill.BackColor.RGB = RGB(250, 0, 0)
s.Fill.ForeColor.RGB = RGB(250, 0, 0)
s.Locked = False                       'This is the crucial part

End Sub

回到我的 WorkBook_Open 子中,我没有在 Protect 功能下调用 DrawingObjects。例如:

Sub Workbook_Open()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
ws.Activate
'Protect the worksheet from being edited
With ws
   .Protect Password:="", UserInterfaceOnly:=True
   .EnableOutlining = True
   .Protect Contents:=True, Scenarios:=True

'Notice how DrawingObjects is not included, thus the default Protect is use

End With

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

现在,每次我的 CommandButton 调用 AddShape 时,都会出现一个可移动和可编辑的新圆圈。我通过“属性”选项卡锁定的任何形状都不可移动也不可编辑。解决了。感谢 dwirony 引导我朝着正确的方向前进。

于 2019-09-14T15:51:42.130 回答