如果系列是图表上的一行(移动到顶部),则重新排列序列号 - VBA / Excel

时间:2014-10-27 16:01:47

标签: excel vba charts

我正在创建一个自动格式化图表的脚本,因为一遍又一遍地重复所有这些步骤有点时间了。我目前有一个脚本可以更改所有系列颜色,线条粗细,重新调整区域大小以及其他一些较小的东西。

Public Sub ChartAlt()
'
' ChartAlt Macro
'
' Keyboard Shortcut: Ctrl+Shift+A
'

    'keeps on chugging if it finds an error (turn off/comment out before editing and testing code)
   On Error Resume Next

    If MsgBox("Have you saved before running this prompt? Saving will allow you to exit and re-open the file to before the changes were made. Macros cannot be undone.", vbYesNo) = vbNo Then Exit Sub

    With ActiveChart
        .HasTitle = True 'turns on title
       .SetElement (msoElementChartTitleAboveChart) 'places title above chart
       .SetElement (msoElementLegendBottom) 'moves legend to bottom
       .HasDataTable = False 'turns off data table
       .ChartArea.Format.Line.Visible = msoFalse 'removes border
       .ShowAllFieldButtons = False ' turns off field buttons (pivot charts only)
   End With

    ' Turns on legend if more than one series exists
   If ActiveChart.SeriesCollection.Count >= 2 Then
        ActiveChart.HasLegend = True
    Else
        ActiveChart.HasLegend = False
    End If

    ' resizes the chart to 7" wide and 4" tall
   With ActiveChart.Parent
        .Height = 288
        .Width = 504
        .Placement = xlFreeFloating
    End With

    ' Changes all Series color purple using incrementing transparencies
   Dim mySeries As Series
    Dim seriesCol As FullSeriesCollection
    Dim i As Integer, J As Variant, UWColor As Long

    i = 1
    J = 1 / (ActiveChart.SeriesCollection.Count + 1) 'creates a percentage transparency based on # of series
   UWColor = RGB(51, 0, 111) 'color taken from UW website

    Set seriesCol = ActiveChart.FullSeriesCollection
    For Each mySeries In seriesCol
        Set mySeries = ActiveChart.FullSeriesCollection(i)
        With mySeries
            .Format.Line.ForeColor.RGB = UWColor
            .Format.Line.Transparency = 0.8 - (i * J) 'a lower 0.X means darker lines
           .Format.Fill.ForeColor.RGB = UWColor
            .Format.Fill.Transparency = 1.2 - (i * J) 'a higher 1.X means lighter fills

            'checks for series type and adjusts line/bar size
           If .ChartType = xlBarStacked Then
                .Format.Line.Weight = 0.5
                ActiveChart.ChartGroups(i).GapWidth = 50
            ElseIf .ChartType = xlBarClustered Then
                .Format.Line.Weight = 0.5
                ActiveChart.ChartGroups(i).GapWidth = 50
            ElseIf .ChartType = xlColumnClustered Then
                .Format.Line.Weight = 0.5
                ActiveChart.ChartGroups(i).GapWidth = 50
            ElseIf .ChartType = xlBarStacked100 Then
                .Format.Line.Weight = 0.5
                ActiveChart.ChartGroups(i).GapWidth = 50
            ElseIf .ChartType = xlLine Then
                .Format.Line.Weight = 2
            ElseIf .ChartType = xlLineMarkers Then
                .Format.Line.Weight = 2
                'Line markers have an issue with colors, this is a temporary solution
               .MarkerBackgroundColorIndex = xlColorIndexAutomatic
                .MarkerForegroundColorIndex = xlColorIndexNone
            Else
                .Format.Line.Weight = 1
            End If

        End With
        i = i + 1
    Next


    ' turns axis on, changed colors black, and adds a line
   With ActiveChart
        For Each a In .Axes
            a.TickLabels.Font.Color = "black"
            a.TickLabels.Font.Size = 10
            a.TickLabels.Font.Bold = False
            a.Format.Line.Visible = msoTrue
            a.Format.Line.ForeColor.ObjectThemeColor = msoThemeColorText1
            a.Format.Line.ForeColor.TintAndShade = 0
            a.Format.Line.ForeColor.Brightness = 0
            a.HasMajorGridlines = False
            a.HadMinorGridlines = False
            a.HasTitle = True
            a.AxisTitle.Format.TextFrame2.TextRange.Font.Fill.Visible = msoTrue
            a.AxisTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 0)
            a.AxisTitle.Format.TextFrame2.TextRange.Font.Fill.Transparency = 0
            a.AxisTitle.Format.TextFrame2.TextRange.Font.Fill.Solid
        Next a
    End With


    ActiveChart.Legend.Select
    With Selection.Format.TextFrame2.TextRange.Font.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(0, 0, 0)
        .Transparency = 0
        .Solid
    End With

    ActiveChart.ChartTitle.Select
    With Selection.Format.TextFrame2.TextRange.Font
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(0, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 16
    End With

End Sub

一个问题是,如果行序列号低于条形序列号,则该行隐藏在条形图后面(在组合图表上)。

有没有办法让脚本识别系列是否为线型,然后将该系列移动到图表的顶部,以便它不会隐藏在任何栏后面?基本上试图说“如果系列是一条线,然后将系列号更改为[系列计数+1]”(我认为)。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我实际想通了......我所要做的就是在“if .ChartType = line”循环下添加.PlotOrder = ActiveSheet.FullSeriesCollection.Count + 1。

在此处查看更新的脚本: http://pastebin.com/c0rV5j3V