从“图表编号 X”重命名图表

问题描述 投票:0回答:1

我有生成图表的代码:

Sheets("kips").Select

Dim i As Integer 'rows
Dim j As Integer 'columns

i = Cells(Rows.Count, 1).End(xlUp).Row

For j = 2 To 5
    With ActiveSheet.Shapes.AddChart.Chart
        .ChartType = xlColumnClustered
        .SeriesCollection.NewSeries
        
        With .SeriesCollection(1)
            '.Name = "=" & ActiveSheet.Name & "!" & _
            'Cells(1, j).Address
            .XValues = "=" & ActiveSheet.Name & "!" & _
            Range(Cells(2, 1), Cells(i, 1)).Address
            .Values = "=" & ActiveSheet.Name & "!" & _
            Range(Cells(2, j), Cells(i, j)).Address
        End With
        
    End With
Next j

我需要创建这四个图表,以便给出一个名称,因为它创建并使用带有“图表编号 X”的内容,而其余代码不起作用,因为我在创建它后立即给它命名.

其余代码。

'Clean the charts
'chart1

ActiveSheet.ChartObjects("Gráfico 12").Activate
ActiveChart.ChartTitle.Select
Selection.Delete
ActiveSheet.ChartObjects("Gráfico 12").Activate
ActiveChart.Axes(xlValue).MajorGridlines.Select
Selection.Delete
ActiveSheet.ChartObjects("Gráfico 12").Activate
ActiveChart.Axes(xlValue).Select
Selection.Delete
ActiveSheet.ChartObjects("Gráfico 12").Activate
ActiveChart.Legend.Select
Selection.Delete

ActiveSheet.ChartObjects("Gráfico 12").Activate
ActiveChart.FullSeriesCollection(1).Select
ActiveChart.ChartGroups(1).Overlap = -27
ActiveChart.ChartGroups(1).GapWidth = 50
With Selection.Format.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 32, 96)
    .Transparency = 0
    .Solid
End With
excel vba excel-charts
1个回答
2
投票
For j = 2 To 5
     With ActiveSheet.Shapes.AddChart.Chart
         .Parent.Name = "Chart_" & (j-1)       '<< name the chartobject (Parent of Chart)
         '...
         '...    

仅供参考,将 ChartObject 直接传递给格式化子可能会更“干净”,而不是为其命名,然后通过名称访问它

For j = 2 To 5
    With ActiveSheet.Shapes.AddChart.Chart
        FormatChart1 .Parent
        '...
        '...    

格式化子:

Sub FormatChart1(co as chartobject)
    With co
        .Chart.ChartTitle.Delete
        .Chart.Axes(xlValue).MajorGridlines.Delete
        .Chart.Axes(xlValue).Delete
        'etc
        'etc
    End with
End sub


© www.soinside.com 2019 - 2024. All rights reserved.