简体   繁体   English

Excel VBA更改数据透视图中的系列颜色

[英]Excel VBA Change Color of Series in Pivot Chart

This is one of those stupid questions that should be really simple, but I can't quite figure out what's wrong, and everything I've found on the Internet hasn't worked. 这是应该非常简单的愚蠢问题之一,但我无法弄清楚到底出了什么问题,而且我在互联网上找到的所有内容都没有用。

I just want to recolor the series line in a pivot chart after I change the filters in the pivot table. 我只想在更改数据透视表中的过滤器后重新着色数据透视表中的系列线。 Here is what I have tried: 这是我尝试过的:

ThisWorkbook.Charts(ChartName).SeriesCollection(SeriesName) _
Format.Line.ForeColor = RGB(75, 172, 198)

That code gives me "subscript out of range" error. 该代码给我“下标超出范围”错误。 I also tried: 我也尝试过:

Sheets(SheetName).ChartObjects(ChartName).Chart.SeriesCollection(SeriesName) _
.Format.Line.ForeColor = RGB(75, 172, 198)

as well as 以及

 ... .ForeColor.ColorIndex = 3

as well as 以及

... .ForeColor.ObjectThemeColor = msoThemeColorAccent4

But those all give me a "type mismatch" error. 但是这些都给我一个“类型不匹配”的错误。 Excel's VBA Recorder gives me: Excel的VBA记录器为我提供:

ActiveChart.SeriesCollection(2).Select
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorAccent4
    .ForeColor.TintAndShade = 0
    .ForeColor.Brightness = 0.400000006
    .Transparency = 0
End With

I'm sure I just have some stupid, obvious error somewhere, but I can't for the life of me figure out how to access the color property of a series in a pivot chart. 我敢肯定我只是在某个地方有一些愚蠢的,明显的错误,但是我一生都无法弄清楚如何在数据透视图中访问一系列的color属性。 Sometimes it feels like trying to navigate a freakin' labyrinth trying to figure out the cryptic hierarchy of objects that a certain property belongs to in VBA... 有时感觉就像试图浏览一个怪兽般的迷宫,试图找出VBA中某个属性所属对象的神秘层次...

What I discover from debugging a simple example is that apparently you cannot assign a .Name property to series in a PivotChart. 从调试中我发现一个简单的示例,显然您不能将.Name属性分配给数据透视图中的series。 I would expect this to raise an error, but it does not. 我希望这会引发错误,但事实并非如此。 It just passes right over the assignment as if nothing happened. 它就好像没有任何反应一样在任务上传递。

Sub Test()
Dim pivotChart As ChartObject
Dim cht As Chart
Dim srs As Series
Dim seriesName As String

Set pivotChart = ActiveSheet.ChartObjects(1)
Set cht = pivotChart.Chart
Set srs = cht.SeriesCollection(1)

'First look at the name:
Debug.Print srs.Name

'Assign a new name to the string variable
seriesName = "Name"

'Assign the new name from string var to the series
srs.Name = seriesName

'Now review that the name has changed. It hasn't!!
Debug.Print ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Name

End Sub

It seems that you have the makings of a solution with your attempts. 您的尝试似乎可以解决问题。 A few things that I found (lots of it in XL Help): 我发现了一些东西(在XL Help中有很多东西):

  1. Format is a read-only property, which might explain why that wasn't working. 格式是只读属性,可以解释为什么它不起作用。
  2. Use ChartGroups instead of SeriesCollection 使用ChartGroups而不是SeriesCollection
  3. From XL Help "There's no object that represents a single series line; you either have series lines turned on for all points in a chart group or you have them turned off." 从XL帮助中:“没有对象代表单个系列线;您可以为图表组中的所有点打开系列线,或者关闭它们。”
  4. Instead of declaring the seriescollection, try using Charts(ChartName).ChartGroup(cg) 而不是声明seriescollection,请尝试使用Charts(ChartName).ChartGroup(cg)

So, your code might look like: 因此,您的代码可能如下所示:

Dim cht as Chart
Dim cg as ChartGroup
Set cht = ActiveSheet.ChartObjects(ChartName)
With cht.ChartGroups(cg)
    .HasSeriesLines = True 'turn on series lines for the chart group
    With .SeriesLines.Border
        .ColorIndex=[pick your color]
    End With
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM