VBA:格式化多个选定图表(图表,绘图,图例等)

我正在寻找使用VBA在Excel 2010上格式化多个选定的图表。 我希望代码能够工作,无论我select一个还是多个图表。 下面的代码在只有一个图表被选中的情况下工作,但是当select了多个图表时,我会得到一个“运行时错误”91“对象variables或带有块variables未设置”。 任何想法如何运行所选图表的数量的macros?

Sub ChartFormat5_Click() ''Adjust chart area ActiveChart.ChartArea.Select 'Size Selection.Width = 631.9 Selection.Height = 290.1 'Border With Selection.Format.Line .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorText1 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Transparency = 0 .Weight = 1 .DashStyle = msoLineSolid End With 'Font With Selection.Format.TextFrame2.TextRange.Font .Name = "Calibri" .Size = 10 .Fill.Visible = msoTrue .Fill.ForeColor.ObjectThemeColor = msoThemeColorText1 .Fill.ForeColor.TintAndShade = 0 .Fill.ForeColor.Brightness = 0 .Fill.Transparency = 0 .Fill.Solid End With ''Adjust axis alignment and format ActiveChart.Axes(xlCategory).Select With Selection.Format.Line .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorText1 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Transparency = 0 End With ActiveChart.Axes(xlCategory).TickLabelSpacing = 1 ActiveChart.Axes(xlCategory).TickLabels.Orientation = 45 ActiveChart.Axes(xlValue).Select Selection.TickLabels.NumberFormat = "#,##0_);(#,##0)" With Selection.Format.Line .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorText1 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Transparency = 0 End With ActiveChart.Axes(xlValue).AxisTitle.Select Selection.Left = 1.5 Selection.Format.Line.Visible = msoFalse ''Adjust legend box ActiveChart.Legend.Select With Selection.Format.Fill .Visible = msoTrue .ForeColor.RGB = RGB(255, 255, 255) .Transparency = 0 .Solid End With With Selection.Format.Line .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorBackground1 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = -0.5 .Transparency = 0 End With Selection.Left = 124 Selection.Top = 67 ''Adjust plot area size and format ActiveChart.PlotArea.Select 'Borders With Selection.Format.Line .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorText1 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Transparency = 0 .Weight = 0.75 .DashStyle = msoLineSolid End With 'Size Selection.Width = ActiveChart.ChartArea.Width - 30.4 Selection.Height = ActiveChart.ChartArea.Height - 8.5 Selection.Top = 4 Selection.Left = 20 'Gridlines ActiveChart.Axes(xlValue).MajorGridlines.Select With Selection.Format.Line .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorText1 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Transparency = 0 End With With Selection.Format.Line .Visible = msoTrue .DashStyle = msoLineDash End With End Sub 

这将处理活动图表或所有选定的图表。 第一个例程确定要处理的内容(活动图表或选定的图表)以及每个第二个进程。

 Sub FormatCharts() Dim obj As Object If Not ActiveChart Is Nothing Then FormatOneChart ActiveChart Else For Each obj In Selection If TypeName(obj) = "ChartObject" Then FormatOneChart obj.Chart End If Next End If End Sub Sub FormatOneChart(cht As Chart) ' do all your formatting here, based on cht not on ActiveChart End Sub 

不要select图表的一部分,只是完全参考它们。 代替

 ActiveChart.ChartArea.Select With Selection.Format.Line 

用这个

 With cht.ChartArea.Format.Line 

等等

刚刚开始回答在stackoverflow的问题,所以我希望这会帮助你。

由于您一次select了多个图表,因此您应该ActiveChart.ChartArea.Select只需按照以下方式循环显示当前select中的每个ChartObject:

 Sub ChartFormat5_Click() Dim cObject As ChartObject For Each cObject In Selection With cObject 'Do all your stuff here... Eg .Chart.PlotArea.Width = 631.9 End With Next cObject End Sub