我有以下代码
Sub PieMarkers()
Dim chtMarker As Chart
Dim chtMain As Chart
Dim intPoint As Integer
Dim rngRow As Range
Dim lngPointIndex As Long
Dim thmColor As Long
Dim myTheme As String
Application.ScreenUpdating = False
Set chtMarker = ActiveSheet.ChartObjects("chtMarker").Chart
Set chtMain = ActiveSheet.ChartObjects("chtMain").Chart
Set chtMain = ActiveSheet.ChartObjects("chtMain").Chart
Set rngRow = Range(ThisWorkbook.Names("PieChartValues").RefersTo)
For Each rngRow In Range("PieChartValues").Rows
chtMarker.SeriesCollection(1).Values = rngRow
ThisWorkbook.Theme.ThemeColorScheme.Load GetColorScheme(thmColor)
chtMarker.Parent.CopyPicture xlScreen, xlPicture
lngPointIndex = lngPointIndex + 1
chtMain.SeriesCollection(1).Points(lngPointIndex).Paste
thmColor = thmColor + 1
Next
lngPointIndex = 0
Application.ScreenUpdating = True
End Sub
Function GetColorScheme(i As Long) As String
Const thmColor1 As String = "C:\Program Files\Microsoft Office\Document Themes 14\Theme Colors\Blue Green.xml"
Const thmColor2 As String = "C:\Program Files\Microsoft Office\Document Themes 14\Theme Colors\Orange Red.xml"
Select Case i Mod 2
Case 0
GetColorScheme = thmColor1
Case 1
GetColorScheme = thmColor2
End Select
End Function
该代码尝试创建一个以饼图作为气泡的气泡图。在这个版本中,颜色主题用于在每个饼图(气泡)中创建不同的颜色。但是,如果没有颜色主题,有任何方法可以做到这一点。我一直使用 Collection 对象来执行此操作,但不知道如何将其实现到代码中。我想我必须更改上述代码的功能部分?
更新代码
Sub PieMarkers()
Dim srs As Series
Dim pt As Point
Dim p As Long
Dim c As Long
Dim col As Long
Dim chtMarker As Chart
Dim chtMain As Chart
Dim intPoint As Integer
Dim rngRow As Range
Dim lngPointIndex As Long
Dim thmColor As Long
Dim myTheme As String
Application.ScreenUpdating = False
Set chtMarker = ActiveSheet.ChartObjects("chtMarker").Chart
Set chtMain = ActiveSheet.ChartObjects("chtMain").Chart
Set chtMain = ActiveSheet.ChartObjects("chtMain").Chart
Set rngRow = Range(ThisWorkbook.Names("PieChartValues").RefersTo)
Set srs = chtMarker.SeriesCollection(1)
For Each rngRow In Range("PieChartValues").Rows
c = c + 1
srs.Values = rngRow
For p = 1 To srs.Points.Count
Set pt = srs.Points(p)
With pt.Format.Fill.ForeColor
col = p + (srs.Points.Count * c)
If col = 1 Then .RGB = 113567
If col = 2 Then .RGB = 116761
If col = 3 Then .RGB = 239403
If col = 4 Then .RGB = 398394
'etc.
'etc.
'## Add more IF statements to assign more colors.
If col = 24 Then .RGB = 1039834
End With
Next
chtMarker.Parent.CopyPicture xlScreen, xlPicture
lngPointIndex = lngPointIndex + 1
chtMain.SeriesCollection(1).Points(lngPointIndex).Paste
Next
lngPointIndex = 0
Application.ScreenUpdating = True
End Sub
所以我可以编译代码的低位而不会出错问题是图表之后只用两种颜色着色(不是代码中指定的5种颜色)。有 8 个饼图,每个饼图有 3 个不同的段。每个段(总共 24 个)应该有不同的颜色,可以通过 RGB 值指示,如第一个答案