简体   繁体   English

如何在没有图表名称的情况下在Excel VBA脚本中引用图表

[英]How do I reference a Chart in an Excel VBA script Without The Chart Name

I am creating an Excel VBA script to format and customize a chart. 我正在创建一个Excel VBA脚本来格式化和自定义图表。 I want to be able to use the script on different charts in different workbooks, so I can't have the chart name like "Chart 1" in the code. 我希望能够在不同工作簿中的不同图表上使用该脚本,因此我在代码中不能具有类似“图表1”的图表名称。 I have seen ways to edit all the charts in a workbook, but I only want to edit the chart that I have clicked on in the workbook. 我已经看到了编辑工作簿中所有图表的方法,但是我只想编辑在工作簿中单击的图表。

The first part of my VBA script, that was created by saving keystrokes, is shown below: As you can see, "Chart 1" is hard coded with each block of code. 通过保存击键创建的VBA脚本的第一部分如下所示:如您所见,“图1”是每个代码块的硬编码。 Is there a way that I can get away from using the chart name because I want to use this macro on different charts with different names. 有没有一种方法可以摆脱使用图表名称的麻烦,因为我想在具有不同名称的不同图表上使用此宏。

Sub Format_Plot_Landscape()
ActiveChart.ChartArea.Select
With ActiveSheet.Shapes("Chart 1").Fill
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorAccent1
    .ForeColor.TintAndShade = 0
    .ForeColor.Brightness = 0
    .Solid
End With
With ActiveSheet.Shapes("Chart 1").Fill
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorBackground1
    .ForeColor.TintAndShade = 0
    .ForeColor.Brightness = 0
    .Transparency = 0
    .Solid
End With
ActiveChart.ChartArea.Select
ActiveChart.PlotArea.Select
ActiveSheet.Shapes("Chart 1").Line.Visible = msoFalse
With Selection.Format.Fill
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorBackground1
    .ForeColor.TintAndShade = 0
    .ForeColor.Brightness = 0
    .Solid
End With
.
.
.

It has been a long time since I created a VBA script in Excel, I used to do it a lot when I wanted to apply specific consistent formatting to different charts. 自从我在Excel中创建VBA脚本以来已经有很长时间了,当我想将特定的一致格式应用于不同的图表时,我经常这样做。 I dont remember the chart name being an issue. 我不记得图表名称是个问题。 In this case even when I try to apply the VBA script to the same chart that I recorded the keystrokes on, I get the error " The item with the specified name wasn't found ." 在这种情况下,即使当我尝试将VBA脚本应用于记录击键的同一图表时,也会收到错误消息“ 找不到具有指定名称的项目 ”。 on the following line of code: 在以下代码行上:

With ActiveSheet.Shapes("Chart 1").Fill

I didn't change the name of the chart. 我没有更改图表的名称。 I am running Office 2013 if that makes any difference. 如果有任何区别,我正在运行Office 2013。

You want to replace your ActiveSheet.Shapes("Chart 1").Fill with something that references to the active chart. 您想用引用活动图表的内容替换ActiveSheet.Shapes("Chart 1").Fill

Microsoft wrote an article about charts: https://msdn.microsoft.com/en-us/library/office/ff194426.aspx that states: "When a chart is the active object, you can use the ActiveChart property to refer to it." Microsoft撰写了有关图表的文章: https : //msdn.microsoft.com/zh-cn/library/office/ff194426.aspx指出:“当图表是活动对象时,可以使用ActiveChart属性引用它”。

From this I would guess you replace your ActiveSheet.Shapes("Chart 1").Fill with ActiveChart.ChartArea.Fill . 由此,我猜想您将ActiveSheet.Shapes("Chart 1").Fill替换为ActiveChart.ChartArea.Fill

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

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