根据表格更新折线图

时间:2013-09-26 16:01:02

标签: excel vba excel-vba excel-2010

每张折线图都有一张Max Min表格。我想构建一个宏,它将更新每个线图(总共40个)。请指教。

这是我到目前为止所拥有的。我确信它写得不好,因为我开始学习如何编写VBA。

Sub Update_Slope()

    With Chart(2).Axes(xlValue, xlPrimary)
        .MaximumScale = ActiveSheet.Range("F58").Value
            ' Constant value
        .MinimumScale = ActiveSheet.Range("F68").Value
            ' Constant Value

    With Chart(4).Axes(xlValue, xlPrimary)
        .MaximumScale = ActiveSheet.Range("F59").Value
            ' Constant value
        .MinimumScale = ActiveSheet.Range("F69").Value
            ' Constant Value
    End With
End Sub

2 个答案:

答案 0 :(得分:0)

UNTESTED

Sub Update_Slope()
    ActiveSheet.ChartObjects("Chart(2)").Activate
    ActiveChart.Axes(xlCategory).MinimumScale = Range("F68").Value
    ActiveChart.Axes(xlCategory).MaximumScale = Range("F58").Value

    ActiveSheet.ChartObjects("Chart(4)").Activate
    ActiveChart.Axes(xlCategory).MinimumScale = Range("F69").Value
    ActiveChart.Axes(xlCategory).MaximumScale = Range("F59").Value
End Sub

答案 1 :(得分:0)

Sub Update_All_Slopes()

    Dim co, sht as WorkSheet

    Set sht = Activesheet
    Set co = sht.ChartObjects

    'ideally you're looping through your table range to do this, instead
    '   of hard-coding each line. Need more info on how your sheet is set up
    '   and how to identify which chart should be updated...
    FixYAxis co("Chart2").Chart,sht.Range("F68").Value, sht.Range("F58").Value
    FixYAxis co("Chart4").Chart,sht.Range("F69").Value, sht.Range("F59").Value
    '...etc

End Sub

Sub FixYAxis(cht as Chart, minVal,maxVal)
    With cht.Axes(xlValue, xlPrimary)
        .MaximumScale = maxVal        
        .MinimumScale = minVal
    End With
End Sub