如何使用VBA为饼图上色 [英] How to use VBA to colour pie chart

查看:162
本文介绍了如何使用VBA为饼图上色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,其中代码尝试创建以饼图为气泡的气泡图.像在此版本中一样,颜色主题用于在功能部件的每个饼图(气泡)中创建不同的颜色,但我有一个问题,即它取决于调色板的路径.

I have the following code in which the code tries to create a bubble chart with pie charts as the bubbles. As in this version colour themes are used to create a different colour in each pie chart (bubble) in the function part I have the problem that it works depending on the paths to the colour palettes.

是否有一种简便的方法可以使功能独立于那些路径而工作,方法是为每个饼图段编码颜色,或者使用标准化路径(可能不可行,不是可取的).

Is there an easy way to make the function in a way that it works independently of those paths either by coding a colour for each pie chart segment or by using standardize paths (probably not possible, not preferable).

    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

该代码在气泡上一次又一次地复制单个图表.因此,我想将Function(现在称为Get colourscheme)更改为一个函数,该函数为每个饼图的每个段分配唯一的RGB颜色. 在此处更改图表excel VBA中的点颜色 a>,但是该代码显然不适用于提出要求的人.有人可以给我任何有关如何重写代码的函数部分的建议

The code copies a single chart again and again on the bubbles. So I would like to alter the Function (now called Get colourscheme) into a function that assigns a a unique RGB colour to each segment of each pie chart. A similar issue is discussed here Change the Point Color in chart excel VBA but the code apparently did not work for the person who was asking. Could anybody give me any advice on how to rewrite the function part of the code

我的粗略做法是:

  1. 选择工作表,然后在复制完每个图形后抓取
  2. 使用唯一的RGB代码更改每个段的颜色

但是我不清楚如何将其实现到VBA中.我真的很感激对此问题的任何评论.

But how I would implement it into VBA is not clear to me. I would really appreciate ANY comments on the issue.

推荐答案

以下是设置饼图中每个切片的颜色的方法.不确定要如何确定哪个切片得到什么颜色.

Here's how to set the colors of each slice in a pie chart. Not sure how you want to determine which slice gets what color.

Dim clr As Long, x As Long

For x = 1 To 30
    clr = RGB(0, x * 8, 0)
    With ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Points(x)
        .Format.Fill.ForeColor.RGB = clr
    End With
Next x

这篇关于如何使用VBA为饼图上色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆