我如何保护图表代码?

时间:2012-04-13 21:43:13

标签: excel vba charts

我试图保持图表在编写后不被更改。我从网上收集了应该执行此操作的代码:

Sub test()
Dim myChartObject As ChartObject
Dim MyChart As Chart

Set myChartObject = ActiveSheet.ChartObjects.Add(Left:=100, Top:=100, Width:=400, Height:=300)

Set MyChart = myChartObject.Chart

MyChart.PlotArea.Width = Application.InchesToPoints(2.583)
MyChart.PlotArea.Height = Application.InchesToPoints(1.75)

MyChart.ChartType = xlLine

MyChart.SetSourceData Source:=ActiveWorkbook.ActiveSheet.Range("A5:D9")
MyChart.Protect Password:="pass", DrawingObjects:=True, Contents:=True

End Sub

我在MyChart.Protect行收到错误。我无法让它发挥作用。

1 个答案:

答案 0 :(得分:1)

我不相信你可以保护图表,除非它也是一张工作表。由于您将图表插入工作表,因此要么保护工作表替换

MyChart.Protect Password:="pass", DrawingObjects:=True, Contents:=True

ActiveSheet.Protect Password:="pass", DrawingObjects:=True, Contents:=True

或将图表作为工作表插入