简体   繁体   English

在图表工作表中放置多个图表时,如何使用vba更改图表属性?

[英]When placing multiple charts in a chart sheet, how do I change chart properties using vba?

I am able to place multiple charts on a chart sheet by creating an empty chart sheet and then setting the Location of the charts to that empty chart sheet. 通过创建一个空的图表表,然后将图表的位置设置为该空的图表表,可以在一个图表表上放置多个图表。

'This creates multiple charts within a single chart sheet!

Charts.Add 'Creates empty chart page when empty cell is selected

'Keep track of chart page for later reference
Dim chartSheet As String
chartSheet = ActiveChart.Name

.... 'Create three separate charts with data here

'Now place these charts within our empty chart page

Set chart1 = chart1.Location(Where:=xlLocationAsObject, Name:=chartSheet)
Set chart2 = chart2.Location(Where:=xlLocationAsObject, Name:=chartSheet)
Set chart3 = chart3.Location(Where:=xlLocationAsObject, Name:=chartSheet)

All of the code works up until this point. 到目前为止,所有代码都可以正常工作。 The chart sheet contains all 3 charts, although the charts are all overlapping. 图表表包含所有3个图表,尽管这些图表都是重叠的。 When I try to adjust the position of the charts... 当我尝试调整图表的位置时...

'This code fails to run!
chart1.Parent.Top = 0
chart1.Parent.Left = 0

The code returns a runtime error of Object doesn't support this property or method . 该代码返回对象不支持此属性或方法的运行时错误。 I know it's possible to move the graphs manually by clicking and dragging, and I know that the above code works if the chart is within a normal worksheet. 我知道可以通过单击和拖动来手动移动图形,并且我知道如果图表在正常工作表中,则上述代码可以工作。 But for some reason, this code fails when the charts are within a chartsheet. 但是由于某种原因,当图表位于图表表中时,此代码将失败。 Is there any way to get VBA to do what I want? 有什么方法可以让VBA做我想做的事情?

Thanks for the help. 谢谢您的帮助。

After a bit of Experimentation I belive your code isn't running because chart1 is it's own worksheet. 经过一番试验后,我相信您的代码没有运行,因为chart1是它自己的工作表。

When I drilled into a selected chart i've noticed the following difference. 当我钻入选定的图表时,我注意到以下差异。 If the chart is embedded (Object in another sheet) the Parent is a Object/ChartObject which has properties Top and Left (it's relative location in the worksheet). 如果嵌入了图表(另一个工作表中的Object/ChartObject ),则Parent Object/ChartObject是具有属性“ Top和“ Left (它在工作表中的相对位置)的Object/ChartObject If the chart is it's own worksheet, the Parent is actually an Object/Thisworkbook' which does not have Top or Left' properties. 如果图表是其自己的工作表,则“ Parent Object/Thisworkbook' which does not have顶部” or左侧”属性的Object/Thisworkbook' which does not have This makes sense because when you create a chartSheet the chart fills the entire sheet area and thus cannot have a relative postion. 这是有道理的,因为当您创建一个chartSheet时,图表会填满整个工作表区域,因此不能有相对位置。

If you insert a breakpoint on chart1.Parent.Top = 0 and view locals.... you can see what I have explained above. 如果您在chart1.Parent.Top = 0上插入一个断点并查看本地语言...。您可以看到我上面解释的内容。

EDIT: to suggest something reagrding your ask in the comments. 编辑:在评论中建议一些引起您询问的内容。

The Children are contained in the 'Shapes' Collection of the worksheet object. 子级包含在工作表对象的“形状”集合中。 i used the following to show the names of all shapes in a worksheet 我使用以下内容在工作表中显示所有形状的名称

For i = 1 To ActiveWorkbook.ActiveSheet.Shapes.Count
    temp = temp & i & " " & ActiveWorkbook.ActiveSheet.Shapes(i).Name & vbCr
Next

MsgBox temp

From there you should be able to move and format etc. 从那里您应该能够移动和格式化等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM