在excel vba 2016中运行循环以更改图表中的系列

时间:2016-01-08 07:16:39

标签: excel vba excel-vba

我有以下宏,我希望从1开始为500个图表循环以下程序。

Sub Arrow()
'
' Arrow Macro
'
' Keyboard Shortcut: Ctrl+q
'
    ActiveSheet.ChartObjects("Chart 459").Activate

    ActiveChart.FullSeriesCollection(1).Select
    With Selection.Format.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(192, 0, 0)
        .Transparency = 0
    End With
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 2.5
    End With
    Selection.Format.Line.EndArrowheadStyle = msoArrowheadTriangle
    With Selection.Format.Line
        .EndArrowheadLength = msoArrowheadLengthMedium
        .EndArrowheadWidth = msoArrowheadWide
    End With
    ActiveChart.FullSeriesCollection(2).Select
    With Selection.Format.Line
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorAccent5
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = -0.5
        .Transparency = 0
    End With
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 2.5
    End With
    Selection.Format.Line.EndArrowheadStyle = msoArrowheadTriangle
    With Selection.Format.Line
        .EndArrowheadLength = msoArrowheadLengthMedium
        .EndArrowheadWidth = msoArrowheadWide
    End With
End Sub

1 个答案:

答案 0 :(得分:1)

我同意@Jeeped。你想要的并不困难。但是,从Select等转移到基于索引需要一些学习。

下面的代码应该做你想要的。它在Office 2010中对我有用,它使用SeriesCollection(1)而不是FullSeriesCollection(1)

Sub Arrow() ' ' Arrow Macro ' ' Keyboard Shortcut: Ctrl+q ' ActiveSheet.ChartObjects("Chart 459").Activate

    Dim i As Long
    Dim cht As Chart

    For i = 1 To ActiveWorkbook.Charts.Count

        Set cht = ActiveWorkbook.Charts(i)

        With cht.FullSeriesCollection(1).Format.Line
            .Visible = msoTrue
            .ForeColor.RGB = RGB(192, 0, 0)
            .Transparency = 0
            .Weight = 2.5
            .EndArrowheadStyle = msoArrowheadTriangle
            .EndArrowheadLength = msoArrowheadLengthMedium
            .EndArrowheadWidth = msoArrowheadWide
        End With

        With cht.FullSeriesCollection(2).Format.Line
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorAccent5
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = -0.5
            .Transparency = 0
            .Visible = msoTrue
            .Weight = 2.5
            .EndArrowheadStyle = msoArrowheadTriangle
            .EndArrowheadLength = msoArrowheadLengthMedium
            .EndArrowheadWidth = msoArrowheadWide
        End With

    Next i

End Sub

现在您知道如何使用基于For循环和索引的引用。