2014-10-27 33 views
0

我正在創建一個自動格式圖表的腳本,因爲重複執行所有這些步驟都是一次又一次一點時間吸吮。我目前有一個腳本,可以改變所有系列顏色,線條粗細,重新調整面積以及其他一些較小的東西。重新排列系列號,如果系列是圖表上的一條線(將線移動到頂部) - VBA/Excel

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]」(我認爲)。

您的幫助表示讚賞。

+0

是的,每個'Series'有一個'.ChartType'屬性,可以檢查該系列是'.ChartType = xlLine'(或修改,以正確的常量/枚舉),並與另一系列中的相應數據點進行比較。每個'DataLabel'都有一個'.Top','.Left','.Width'和'.Height'屬性,所以你可以簡單地對這些屬性進行一些調整,以便在圖表中的其他地方移動標籤。 – 2014-10-27 17:32:11

+0

我認爲還有其他的方式來移動數據標籤,但這是首先想到的。我不再在Excel中進行大量開發,但是如果您在代碼中發佈代碼,或許我(或其他人)可以提供幫助。 – 2014-10-27 17:33:06

+0

其實我想通了...... 所有我要做的就是在 .PlotOrder = ActiveSheet.FullSeriesCollection.Count + 1 下添加「如果.ChartType =行」循環。 – user3486216 2014-10-27 19:09:49

回答

0

我真的想通了......我所要做的只是在「if .ChartType = line」循環下添加.PlotOrder = ActiveSheet.FullSeriesCollection.Count + 1。

見更新腳本在這裏: http://pastebin.com/c0rV5j3V

相關問題