excel - VBA ChartObject 更改图例中系列的名称

标签 excel vba charts series

我正在开发一个仪表板,该仪表板有两张表,一张名为“当前 DMB 模板”,第二张名为“KPI”。在主选项卡中,有两列绿色一列 D8:E27 和红色一列 G8:H27。 enter image description here

KPI 中有 4 个表。

enter image description here

感谢这里另一位成员的帮助,我有一个宏,它将根据 KPI 是否实现在绿色或红色列中为每个表创建一个图表:

Sub DeleteKPICharts()

    On Error Resume Next

    Worksheets("Current DMB Template").ChartObjects.Delete

End Sub

Sub KPIcharts()
Dim wb As Workbook
Dim main As Worksheet
Dim KPI As Worksheet

Dim embeddedchart As ChartObject
Set wb = ThisWorkbook
Set main = wb.Sheets("Current DMB Template")
Set KPI = wb.Sheets("KPI")

Dim TotalGraphsD As Byte
Dim TotalGraphsG As Byte
Dim i As Long, j As Long
Dim ThisColumn As String


For i = 2 To 11 Step 3 'loop trough 2-8 in breaks of 3-> 2,5,8
    ThisColumn = IIf(KPI.Range("E" & i).Value = "No", "G", "D") 'which column will be the graph
    j = 9 + (7 * IIf(ThisColumn = "G", TotalGraphsG, TotalGraphsD))  'which row will be the graph, first in row 9 in steps of 7 so, first in row 9, second row 16, third row 23 and so on...
    Set embeddedchart = main.ChartObjects.Add(Left:=Range(ThisColumn & j).Left, Width:=170, Top:=Range(ThisColumn & j).Top, Height:=100)
    embeddedchart.Chart.SetSourceData Source:=KPI.Range("A" & i & ":C" & i)
    
    If ThisColumn = "G" Then TotalGraphsG = TotalGraphsG + 1 Else TotalGraphsD = TotalGraphsD + 1
Next i


End Sub

现在我面临另一个问题。在每种情况下,每个图表都有一个图例,表示系列 1 和系列 2。

enter image description here

我尝试使用 .SeriesNameLevel = xlSeriesNameLevelAll 更改名称,但收到有关错误方法/属性的消息。请帮忙。我需要将系列分别命名为“实现”和“目标”。 非常感谢。

最佳答案

试试这个:

Sub test()
Dim wb As Workbook
Dim main As Worksheet
Dim kpi As Worksheet

Dim embeddedchart As ChartObject
Set wb = ThisWorkbook
Set main = wb.Sheets("Current DMB Template")
Set kpi = wb.Sheets("KPI")

Dim TotalGraphsD As Byte 'will store how many graphs in D
Dim TotalGraphsG As Byte ' will store how many graphs in G
Dim i As Long, j As Long
Dim ThisColumn As String


For i = 1 To 25 Step 3 'loop trough 1-25 in breaks of 3-> 1,4,7
    ThisColumn = IIf(kpi.Range("E" & (i + 1)).Value = "no", "G", "D") 'which column will be the graph
    j = 9 + (7 * IIf(ThisColumn = "G", TotalGraphsG, TotalGraphsD))  'which row will be the graph, first in row 9 in steps of 7 so, first in row 9, second row 16, third row 23 and so on...
    Set embeddedchart = main.ChartObjects.Add(Left:=Range(ThisColumn & j).Left, Width:=170, Top:=Range(ThisColumn & j).Top, Height:=100)
    With embeddedchart.Chart
        .SetSourceData Source:=kpi.Range("B" & i & ":C" & (i + 1)) 'source data
        .ChartWizard , xlColumn, , xlColumns, , , True 'plot by cols
        .SeriesCollection(1).XValues = "=" & kpi.Range("A" & (i + 1)).Address(True, True, xlA1, True, True) 'title
    End With
    If ThisColumn = "G" Then TotalGraphsG = TotalGraphsG + 1 Else TotalGraphsD = TotalGraphsD + 1
Next i


End Sub

enter image description here

请注意,我已经编辑了循环和部分内部代码。此外,最新 Excel 版本的预期输出可能有所不同(我的是 2007 年...)。

我使用了一些有趣的对象来实现这一点:

ChartObjects.Add method (Excel)

Chart.ChartWizard method (Excel)

关于excel - VBA ChartObject 更改图例中系列的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76062070/

相关文章:

vba - Excel范围到平面字符串

c# - 单元格中的 Excel XML 换行符

javascript - Angular 图表不显示

javascript - Charts.JS - 图表太多? - 加载问题

charts - ASP.Net 图表和 MVC

vba - 自动将选择复制到新文件并自动保存的代码

excel - 使用公式更改小数分隔符

vba - 如何使用 Excel VBA 和用户窗体编辑嵌入在 Excel 中的 PowerPoint 演示文稿

vba - "Application.Quit"让 Excel 在后台运行

vba - Excel VBA : Formula Not Entering Correctly From String