0

我有一个表格,每个折线图都有 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
4

2 回答 2

0

未经测试

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
于 2013-09-26T16:49:05.263 回答
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
于 2013-09-26T18:14:09.200 回答