Chart 对象 (Excel)Chart object (Excel)

代表工作簿中的图表。Represents a chart in a workbook.


图表可以是内嵌图表(嵌入 ChartObject 对象中),也可以是单独的图表工作表。The chart can be either an embedded chart (contained in a ChartObject object) or a separate chart sheet.

Charts 集合包含工作簿中每个图表工作表的 Chart 对象。The Charts collection contains a Chart object for each chart sheet in a workbook. Charts(index)(其中 index 是图表工作表索引号或名称)可用于返回一个 Chart 对象。Use Charts (index), where index is the chart-sheet index number or name, to return a single Chart object.

图表 index 号表示图表工作表在工作簿标签栏上的位置。The chart index number represents the position of the chart sheet on the workbook tab bar. Charts(1) 是工作簿中的第一个(最左侧)图表,Charts(Charts.Count) 是最后一个(最右侧)图表。Charts(1) is the first (leftmost) chart in the workbook; Charts(Charts.Count) is the last (rightmost).

所有图表工作表都计入索引计数,即使处于隐藏状态,也不例外。All chart sheets are included in the index count, even if they are hidden. 图表工作表名称显示在图表的工作簿标签上。The chart-sheet name is shown on the workbook tab for the chart. ChartObject 对象的 Name 属性可用于设置或返回图表名称。You can use the Name property of the ChartObject object to set or return the chart name.

下面的示例在图表工作表 1 中更改系列 1 的颜色。The following example changes the color of series 1 on chart sheet 1.

Charts(1).SeriesCollection(1).Format.Fill.ForeColor.RGB = rgbRed

下面的示例将名为“Sales”的图表移到活动工作簿的末尾。The following example moves the chart named Sales to the end of the active workbook.

Charts("Sales").Move after:=Sheets(Sheets.Count)

Chart 对象也是 Sheets 集合的成员。此集合包含工作簿中的所有工作表(图表工作表和工作表)。The Chart object is also a member of the Sheets collection, which contains all the sheets in the workbook (both chart sheets and worksheets). Sheets(index)(其中 index 是工作表索引号或名称)可用于返回一个表。Use Sheets (index), where index is the sheet index number or name, to return a single sheet.

如果图表是活动对象,可以使用 ActiveChart 属性引用它。When a chart is the active object, you can use the ActiveChart property to refer to it. 如果用户已选择图表工作表,或已使用 Chart 对象的 Activate 方法或 ChartObject 对象的 Activate 方法激活了图表工作表,图表工作表就是活动对象。A chart sheet is active if the user has selected it or if it has been activated with the Activate method of the Chart object or the Activate method of the ChartObject object.

下面的示例激活图表工作表 1,并设置图表类型和标题。The following example activates chart sheet 1, and then sets the chart type and title.

With ActiveChart 
 .Type = xlLine 
 .HasTitle = True 
 .ChartTitle.Text = "January Sales" 
End With

如果用户已选择内嵌图表,或已使用 Activate 方法激活内嵌图表中包含的 ****ChartObject**** 对象,内嵌图表就是活动对象。An embedded chart is active if the user has selected it, or the ChartObject object in which it is contained has been activated with the Activate method.

下面的示例激活工作表 1 中的内嵌图表 1,并设置图表类型和标题。The following example activates embedded chart 1 on worksheet 1 and then sets the chart type and title. 请注意,在激活内嵌图表后,此示例中的代码与上一示例相同。Notice that after the embedded chart has been activated, the code in this example is the same as that in the previous example. 使用 ActiveChart 属性,可以写入 Visual Basic 代码,从而引用内嵌图表或图表工作表(以活动对象为准)。Using the ActiveChart property allows you to write Visual Basic code that can refer to either an embedded chart or a chart sheet (whichever is active).

ActiveChart.ChartType = xlLine 
ActiveChart.HasTitle = True 
ActiveChart.ChartTitle.Text = "January Sales"

如果图表工作表是活动工作表,可以使用 ActiveSheet 属性引用它。When a chart sheet is the active sheet, you can use the ActiveSheet property to refer to it. 下面的示例使用 Activate 方法激活名为 Chart1 的图表工作表,并将图表中系列 1 的内部颜色设置为蓝色。The following example uses the Activate method to activate the chart sheet named Chart1, and then sets the interior color for series 1 in the chart to blue.

ActiveSheet.SeriesCollection(1).Format.Fill.ForeColor.RGB = rgbBlue




另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.