如果序列是图表上的一行(将行移到顶部),请重新排列序列号-VBA/Excel
我正在创建一个自动格式化图表的脚本,因为反复执行所有这些步骤有点耗时。我目前有一个脚本,可以更改所有系列的颜色、线条厚度、重新调整区域大小以及其他一些较小的内容如果序列是图表上的一行(将行移到顶部),请重新排列序列号-VBA/Excel,excel,vba,charts,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 test
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
一个问题是,如果有一个线序列号低于条形图序列号,则在组合图上,该线隐藏在条形图的后面
有没有办法让脚本识别序列是否为线型,然后将该序列移动到图表顶部,使其不会隐藏在任何栏后面?基本上是说,如果序列是一条线,那么我想把序列号改为[series count+1]
非常感谢你的帮助。我真的想出来了。。。我所要做的就是在if.ChartType=line循环下添加.PlotOrder=ActiveSheet.FullSeriesCollection.Count+1 请参见此处更新的脚本:
是的,每个系列都有一个.ChartType属性,您可以检查该系列是否为.ChartType=xlLine或修改为正确的常量/枚举,并与另一系列中的相应数据点进行比较。每个数据标签都有一个.Top、.Left、.Width和.Height属性,因此您可以简单地对这些属性进行一些调整,以便将标签移动到图表中的其他位置。我认为还有其他方法可以移动数据标签,但这是首先想到的方法。我不再在Excel中做很多开发工作,但是如果你被困在这里发布你的代码,也许我或其他人可以帮助你。我真的找到了答案。。。我所要做的就是在if.ChartType=line循环下添加.PlotOrder=ActiveSheet.FullSeriesCollection.Count+1。