使用VBA获取图表名称

我正在设置一个macros来生成一个图表。 我在生成示例图表时logging了一个macros,但是现在我需要让macros的工作与图表名称无关(本例中的Chart 9

 Sheets("statistics").Select Sheets("statistics").Range("A101:C106").Select ActiveSheet.Shapes.AddChart.Select ActiveChart.ChartType = xlColumnStacked ActiveChart.SetSourceData Source:=Range("statistics!$A$101:$C$106") ActiveChart.ChartArea.Select ActiveSheet.Shapes("Chart 9").Name = "waterfall" ActiveChart.Location Where:=xlLocationAsObject, Name:="summary" ActiveSheet.ChartObjects("waterfall").Activate ActiveSheet.Shapes("waterfall").IncrementLeft 80 ActiveSheet.Shapes("waterfall").IncrementTop -2200 ActiveSheet.ChartObjects("waterfall").Activate ActiveSheet.Shapes("waterfall").ScaleWidth 1.6025463692, msoFalse, msoScaleFromTopLeft ActiveSheet.Shapes("waterfall").ScaleHeight 1.6084106153, msoFalse, msoScaleFromTopLeft ActiveSheet.ChartObjects("waterfall").Activate ActiveChart.Legend.Select Selection.Delete ActiveSheet.ChartObjects("waterfall").Activate ActiveChart.SeriesCollection(1).Select Selection.Format.Fill.Visible = msoFalse ActiveChart.SeriesCollection(2).Select ActiveChart.SeriesCollection(2).Points(6).Select With Selection.Format.Fill .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorAccent3 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Solid End With ActiveChart.SeriesCollection(2).Points(1).Select With Selection.Format.Fill .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorAccent3 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Solid End With With Selection.Format.Fill .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorAccent1 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Transparency = 0 .Solid End With ActiveChart.SeriesCollection(2).Points(5).Select With Selection.Format.Fill .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorAccent1 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Solid End With With Selection.Format.Fill .Visible = msoTrue .ForeColor.RGB = RGB(255, 0, 0) .Transparency = 0 .Solid End With ActiveChart.SetElement (msoElementDataLabelCenter) ActiveChart.SeriesCollection(2).Points(1).Select ActiveChart.ChartArea.Select ActiveChart.ChartArea.Select ActiveChart.SeriesCollection(2).Points(1).Select ActiveChart.PlotArea.Select ActiveChart.SeriesCollection(2).Select ActiveChart.SetElement (msoElementDataLabelCenter) ActiveChart.SetElement (msoElementPrimaryValueAxisTitleHorizontal) Selection.Caption = "hrs" ActiveChart.Axes(xlValue).AxisTitle.Select Selection.Left = 7 Selection.Top = 13.028 

我努力了

 Sheets("statistics").Range("A101:C106").Select ActiveSheet.Shapes.AddChart.Select ActiveChart.ChartType = xlColumnStacked ActiveChart.SetSourceData Source:=Range("statistics!$A$101:$C$106") ActiveChart.ChartArea.Select Set ThisChart = ActiveChart ActiveSheet.Shapes(ThisChart).Name = "waterfall" 

但它不工作

尝试下面的代码,它将遍历“statistics”工作表中所有现有的ChartObjects ,如果它find名为“Chart 9”的图表对象,则将它重命名为“waterfall”。

注意 :您可以使用类似的方法来创build图表,而无需使用SelectActiveSheetActiveChart

 Option Explicit Sub RenameExistingChart() Dim ChtObj As ChartObject For Each ChtObj In Worksheets("statistics").ChartObjects If ChtObj.Name = "Chart 9" Then ChtObj.Name = "waterfall" End If Next ChtObj End Sub 

编辑1 :用ChtObj创build图表:

 Set ChtObj = Worksheets("statistics").ChartObjects.Add(Left:=100, Top:=100, _ Width:=100, Height:=100) ' <-- just default settings , modify later With ChtObj .Chart.ChartType = xlColumnStacked .Chart.SetSourceData Source:=range("statistics!$A$101:$C$106") .Name = "waterfall" With .Chart.SeriesCollection(2).Format.Fill ' modify fill for series (2) .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorAccent3 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Solid End With .Chart.SeriesCollection(1).ApplyDataLabels ' add data lables to series (1) End With 

你可以使用这样的东西:

 Sub ChartStuff() Dim cht As Shape Range("A101:A106").Select ActiveSheet.Shapes.AddChart.Select Set cht = ActiveSheet.Shapes(1) cht.Name = "waterfall" End Sub 

希望这可以帮助!

处理VBA中的图表有点复杂。 当您使用Addchart ,select将是ChartArea ChartArea是属于ChartObject一部分的Chart的一部分您看到的图表的名称实际上是ChartObject的名称

你可以做这样的事情:

 Range("A101:A106").Select ActiveSheet.Shapes.AddChart.Select Dim ca As ChartArea, ch As Chart, co As ChartObject Set ca = Selection Set ch = ca.Parent ch.ChartType = xl3DColumn Set co = ch.Parent co.Name = "waterfall" Debug.Print ca.Name, ch.Name, co.Name