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

时间:2014-11-17 09:59:11

标签: excel vba excel-vba

我想用一个单元格中的宏自动更改图表轴的值。如果命令按钮和图表在同一张纸上,我可以让它工作。但是我想在图表上更改它,这不是在普通表格中,而是在图表表格中#34;,所以引用有点不同。现在有人怎么样?

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

1 个答案:

答案 0 :(得分:2)

您必须使用适当的参考。例如(未经测试

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