VBA运行时错误1004设置最小和最大图表轴值

问题描述 投票:0回答:2

我有一个MS Acces Db。我有一个命令按钮导出(TransferSpreadsheet acExport)查询到Excel并创建一个图表(设置ch = ws.Shapes.AddChart.Chart)

这是我的代码工作正常:

With ch
  .ChartType = xlColumnClustered
  .SeriesCollection(2).AxisGroup = 2
  .SeriesCollection(2).ChartType = xlLineMarkers
  .ChartGroups(1).GapWidth = 69
  .Axes(xlValue).MajorGridlines.Delete
  .Axes(xlCategory, xlPrimary).HasTitle = False
  .Axes(xlValue, xlPrimary).HasTitle = False
  .SetElement (msoElementLegendBottom)
End with

我需要使用vba设置图表的轴最大值和最小值。我需要参考工作表中已有的图表。

每次我添加到我的代码:

.MaximumScale = ActiveSheet.Range("Axis_max").Value
.MinimumScale = ActiveSheet.Range("Axis_min").Value

VBA显示ERROR 1004“应用程序定义或对象”我很困惑为什么我的代码不运行这些指令

如果有人能给我一个引导,我真的很感激,

祝一切顺利

excel-vba charts max axis min
2个回答
0
投票

你的问题是.MinimumScale.MaximumScale不是Chart的属性,而是Axis对象的属性。

尝试改为:

.Axes(xlCategory, xlPrimary).MaximumScale = Sheet1.Range("Axis_max").Value
.Axes(xlCategory, xlPrimary).MinimumScale = Sheet1.Range("Axis_min").Value

0
投票

最后,我找到了一个设置最小和最大垂直图表轴的代码。

With ch
.ChartType = xlColumnClustered
.SeriesCollection(2).AxisGroup = 2
.SeriesCollection(2).ChartType = xlLineMarkers
.ChartGroups(1).GapWidth = 69

myMax = DMax("Total_Sal", "qry_task")
myMin = DMin("Total_Sal", "qry_task")

 With .Axes(xlvalue, xlPrimary)
        .MinimumScale = myMin
        .MaximumScale = myMax
 End With

myMax = DMax("Task_Val", "qry_task")
myMin = DMin("Task_Val", "qry_task")
With .Axes(xlvalue, xlSecondary)
        .MinimumScale = myMin
        .MaximumScale = myMax
    End With

结束

我非常感谢ja72的初步帮助

为了完成我的代码,我得到了Andy Pope的帮助

© www.soinside.com 2019 - 2024. All rights reserved.