在布尔值 vba 中重命名图表

rename charts in a boolean vba

你好吗? 有人可以帮助我吗?

我有以下生成图形的代码:

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

它运行良好,但我需要创建这 4 个图形,并给出一个名称,因为它创建并使用了带有“图形编号 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
For j = 2 To 5
     With ActiveSheet.Shapes.AddChart.Chart
         .Parent.Name = "Chart_" & (j-1)       '<< name the chartobject (Parent of Chart)
         '...
         '...    

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

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