VBAexcel。如何通过图表名称访问图表比例尺?
VBA excel. How to get access to chart scale by chart name?
貌似很简单,就是想不通如何按名称操作图表。
例如这段代码工作正常:
Sub scale_chart()
Dim chart_name As String
chart_name = "Chart 3"
ActiveSheet.ChartObjects(chart_name).Select
With ActiveChart.Axes(xlValue)
.MaximumScale = 800000
End With
End Sub
但下面的代码提供了“运行-time error '438' Object doesn't support proprty or method”
Sub scale_chart()
Dim chart_name As String
chart_name = "Chart 3"
With ActiveSheet.Charts(chart_name).Axes(xlValue)
.MaximumScale = 800000
End With
End Sub
和
Sub scale_chart()
Dim chart_name As String
chart_name = "Chart 3"
With ActiveSheet.ChartObjects(chart_name).Charts.Axes(xlValue)
.MaximumScale = 800000
End With
End Sub
我尽量避免 .select 因为它在这里推荐
How to avoid using Select in Excel VBA
但还是需要更多的知识。
如果有人指出我做错了什么,我将不胜感激:)
p.s.: 是 VBA Excel 2010
Try,
Sub scale_chart()
Dim chart_name As String
Dim Ws As Worksheet
Dim objCht As ChartObject
Dim Cht As Chart
Set Ws = ActiveSheet
chart_name = "Chart 3"
Set objCht = Ws.ChartObjects(chart_name)
Set Cht = objCht.Chart
With Cht.Axes(xlValue)
.MaximumScale = 800000
End With
End Sub
貌似很简单,就是想不通如何按名称操作图表。
例如这段代码工作正常:
Sub scale_chart()
Dim chart_name As String
chart_name = "Chart 3"
ActiveSheet.ChartObjects(chart_name).Select
With ActiveChart.Axes(xlValue)
.MaximumScale = 800000
End With
End Sub
但下面的代码提供了“运行-time error '438' Object doesn't support proprty or method”
Sub scale_chart()
Dim chart_name As String
chart_name = "Chart 3"
With ActiveSheet.Charts(chart_name).Axes(xlValue)
.MaximumScale = 800000
End With
End Sub
和
Sub scale_chart()
Dim chart_name As String
chart_name = "Chart 3"
With ActiveSheet.ChartObjects(chart_name).Charts.Axes(xlValue)
.MaximumScale = 800000
End With
End Sub
我尽量避免 .select 因为它在这里推荐
How to avoid using Select in Excel VBA
但还是需要更多的知识。
如果有人指出我做错了什么,我将不胜感激:)
p.s.: 是 VBA Excel 2010
Try,
Sub scale_chart()
Dim chart_name As String
Dim Ws As Worksheet
Dim objCht As ChartObject
Dim Cht As Chart
Set Ws = ActiveSheet
chart_name = "Chart 3"
Set objCht = Ws.ChartObjects(chart_name)
Set Cht = objCht.Chart
With Cht.Axes(xlValue)
.MaximumScale = 800000
End With
End Sub