VBA:格式化多个选定的图表 [英] VBA: Formatting Multiple Selected Charts
问题描述
我希望使用VBA在Excel 2010上格式化多个选定的图表。下面的代码只有在选择了一个图表时才可以使用,但是当选择了多个图表时,我得到一个运行时错误91对象变量或未设置块变量。任何想法如何为选定图表的数量运行宏?
Sub ChartFormat5_Click()
''调整图表区域
'大小
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
结束
'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 Sub
谢谢!
这将处理活动图表或所有选定的图表。第一个例程确定要处理的内容(活动图表或选定的图表)和第二个进程。
Sub FormatCharts()
Dim obj As Object
如果不是ActiveChart是没有
FormatOneChart ActiveChart
Else
对于每个obj在选择
如果TypeName(obj)= ChartObject然后
FormatOneChart obj.Chart
End If
Next
End If
End Sub
Sub FormatOneChart(cht As Chart)
'根据cht不在ActiveChart
End Sub
不要选择图表的部分,只需完全引用它们。而不是
ActiveChart.ChartArea.Select
With Selection.Format.Line
使用此
使用cht.ChartArea
$ p $注意:这是 VBA:格式化多个选定图表的重复(图,情节,图例等)
I am looking to format multiple selected charts on Excel 2010 using VBA. The code below works when only one chart is selected but when multiple charts are selected, I get a "run-time error '91' Object variable or With Block variable not set". Any idea how to run the macro for number of selected charts?
Sub ChartFormat5_Click()
''Adjust chart area
'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
End Sub
Thanks!
解决方案 This will process the active chart or all selected charts. The first routine determines what to process (active chart or selected charts) and the second processes each.
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
Don't select parts of the chart, just fully reference them. Instead of
ActiveChart.ChartArea.Select
With Selection.Format.Line
use this
With cht.ChartArea.Format.Line
etc.
Note: this is a duplicate of VBA: Formatting Multiple Selected Charts (Chart, Plot, Legend, etc.)
这篇关于VBA:格式化多个选定的图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!