我正在尝试使用vba设置图表的最大值和最小值,引用2个单元格。我正在尝试参考已在工作簿中的此图表。以下代码:
Sub ScaleAxes()
Dim Sht1 As Worksheet
Dim Sht1Name As String
Set Sht1 = Worksheets("Lists")
With Application.ActiveChart.Axes(xlValue, xlPrimary)
.MinimumScale = Sht1.Range("L2").Value
.MaximumScale = Sht1.Range("L3").Value
End With
End Sub
如果我选择了图表,代码就可以工作,但如果我没有点击图表,我该如何使其运作?
提前致谢,
最高
答案 0 :(得分:2)
使用代码定义MyChtObj
,稍后将Sht1
设置为Option Explicit
Sub ScaleAxes()
Dim Sht1 As Worksheet
Dim Sht1Name As String
Set Sht1 = Worksheets("Lists")
Dim MyChtObj As ChartObject
Set MyChtObj = Sht1.ChartObjects("Chart 1") ' <-- modify "Chart 1" to your chart's name
With MyChtObj.Chart.Axes(xlValue, xlPrimary)
.MinimumScale = Sht1.Range("L2").Value
.MaximumScale = Sht1.Range("L3").Value
End With
End Sub
中的图表名称(修改&#34; Chart1&#34;到您的图表&# 39; s name)。
switch