简体   繁体   中英

Excel-VBA: editing the axes of a chart on a protected worksheet

i've been trying and failing on this for quite some time now...

I have an excel sheet in which the user can copy data. The sheet already contains a chart that is automatically filled with the input-data. There is a button which takes values from the sheet (in M8 and M9) to scale the chart with the following makro:

With ActiveSheet.ChartObjects("Diagramm 2").Chart

    .Axes(xlValues, xlPrimary).MaximumScale = Sheets("Sheet1").Range("M8").Value
    .Axes(xlValues, xlPrimary).MinimumScale = Sheets("Sheet1").Range("M9").Value

End With

This give me the error "the method 'axes' of the object "_Chart" has failed".

The Sheet is protected, but the option "edit objects" in the protection-dialog is checked. The diagram is also set to be not protected.

Is there any way besides surrounding the code with

ActiveSheet.Unprotect
'code here
ActiveSheet.Protect

because the password might change afterwards and the user should not have to edit the vba-code.

Any ideas or input is appreciated. Thanks, Paul

One possible workaround that i found could be the following (found at: http://answers.microsoft.com/en-us/office/forum/office_2010-customize/vba-error-for-chart-on-protected-sheet/b7cf62ca-6c08-4ee9-a596-d457be84fd95 ):

Add another sheet ("Sheet2") that is not protected but hidden. In this sheet create the diagram based on the data from "Sheet1". Copy the diagram from "Sheet2" and in "Sheet 1" go Paste->Image. Now select the image and add a formular like "=Sheet2!A1:G12". This displays what you would see on "Sheet2" in A1:G12 (of course you'd probably need to move the diagram and/or change the range). Edit the makro from above to this:

Sheets("Sheet2").ChartObjects("Diagramm 2").Activate
ActiveChart.ChartArea.Select
ActiveChart.Axes(xlValue).Select

With ActiveChart.Axes(xlValue)
   .MaximumScale = Sheets("Sheet1").Range("M8").Value
   .MinimumScale = Sheets("Sheet1").Range("M9").Value
End With

When you now change the diagram in the (hidden) sheet "Sheet2" the image on "Sheet1" should be automatically updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM