Naming an Excel ChartA common question people have is "How can I name an Excel chart?" When using VBA, you can activate a chart by name, but charts just get a default name like "Chart 3" or "Chart 88", in the order in which they are created. A recorded macro will capture that sequential name, but it's no good when you want to run the macro on another chart. Here are some techniques for naming charts. Chart SheetsA chart sheet has a name just like any other sheet. You name the sheet as follows. Manually - Existing Chart Sheet:Select the sheet. Double click on the sheet tab, type whatever name you want, and press Enter. VBA - Active Chart Sheet:ActiveChart.Name = "Name of this Chart" VBA - Any Existing Chart Sheet:ActiveWorkbook.Charts(3).Name = "Name of this Chart" or... ActiveWorkbook.Charts("Old Chart Name").Name = "Name of this Chart" VBA - While Creating a New Chart:Charts.Add ActiveChart.ChartType = xlXYScatterLines ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A3:G14") ActiveChart.Name = "Name of this Chart" Embedded ChartsAn embedded chart has no name property; the parent Chart Object has the name. You can name an embedded chart's parent chart object this way:
Manually - Existing Chart:Select a cell. Hold Shift or Ctrl while clicking on the chart. The chart has white handles, not black (handles are those little squares on the corners and midpoints of the edges).
Click in the Name Box (above the top left visible cell, to the left of the Formula Bar), where it probably says something like "Chart 3", and type whatever name you want, and press Enter.
VBA - Active Chart:ActiveChart.Parent.Name = "Name of this Chart" VBA - Any Existing Chart:ActiveSheet.ChartObjects(3).Name = "Name of this Chart" or... ActiveSheet.ChartObjects("Old Chart Name").Name = "Name of this Chart" VBA - While Creating a New Chart:If you're adding a chart (sheet) then locating it on a worksheet: Charts.Add ActiveChart.ChartType = xlXYScatterLines ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A3:G14") ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1" ActiveChart.Parent.Name = "Name of this Chart" If you're adding an embedded chart to a worksheet: With ActiveSheet.ChartObjects.Add _ (Left:=100, Width:=375, Top:=75, Height:=225) With .Chart .ChartType = xlXYScatterLines .SetSourceData Source:=Sheets("Sheet1").Range("A3:G14") .Parent.Name = "Name of this Chart" End With End With |
Peltier Technical Services, Inc.Excel Chart Add-Ins | Training | Charts and Tutorials | PTS BlogPeltier Technical Services, Inc., Copyright © 2017. All rights reserved. |