使用macros更改图表工作表的轴

我想用macros从一个单元格自动更改图表轴的值。 如果命令button和图表在同一张纸上,我可以使它工作。 但是我想在图表上改变它,这不是在一个正常的表格中,而是在一个“图表”中,所以引用是有点不同的。 现在有人怎么样?

Sub ChangeAxisScale() With ActiveSheet.ChartObjects("chart21").Chart With .Axes(xlValue) .MaximumScale = ActiveSheet.Range("Axis_max").Value .MinimumScale = ActiveSheet.Range("Axis_min").Value .MajorUnit = ActiveSheet.Range("Unit").Value End With End With End Sub 

你必须使用适当的参考。 例如( 未经testing

 Sub ChangeAxisScale() Dim wsChart As Chart Dim wsInput As Worksheet '~~> Change the below as applicable Set wsChart = Chart1 '<~~ Code name of the chart sheet Set wsInput = ThisWorkbook.Sheets("Sheet1") '<~~ Name of sheet with data With wsChart With .Axes(xlValue) .MaximumScale = wsInput.Range("Axis_max").Value .MinimumScale = wsInput.Range("Axis_min").Value .MajorUnit = wsInput.Range("Unit").Value End With End With End Sub