繁体   English   中英

Excel VBA 使用条件格式从单元格填充饼图颜色

[英]Excel VBA to fill pie chart colors from cells with conditional formatting

我没有使用 VBA 的经验,我正在尝试根据 Excel 2010 中数据单元格的颜色在一个活动工作表上设置所有饼图的格式。我从以下位置找到了此代码: http : //datapigtechnologies.com/ blog/index.php/color-pie-chart-slices-to-match-their-source-cells/

Sub ColorPies()
Dim cht As ChartObject
Dim i As Integer
Dim vntValues As Variant
Dim s As String
Dim myseries As Series

    For Each cht In ActiveSheet.ChartObjects
        For Each myseries In cht.Chart.SeriesCollection

            If myseries.ChartType <> xlPie Then GoTo SkipNotPie
            s = Split(myseries.Formula, ",")(2)
            vntValues = myseries.Values

            For i = 1 To UBound(vntValues)
                myseries.Points(i).Interior.Color = Range(s).Cells(i).Interior.Color
            Next i
SkipNotPie:
    Next myseries
Next cht
End Sub

此代码运行良好,但无法从条件格式中获取颜色。

我遇到了这个 VBA 读取条件格式颜色的解决方案:

Selection.FormatConditions(1).BarColor.Color

但是我一直无法在上面的 VBA 块中实现它。 我尝试用它的不同部分替换 Interior.Color ,但似乎都不起作用。 有谁知道一个简单的方法来做到这一点?

先感谢您!

由于您有 2010,您可以使用 DisplayFormat 属性:

        For i = 1 To UBound(vntValues)
            myseries.Points(i).Interior.Color = Range(s).Cells(i).DisplayFormat.Interior.Color
        Next i

暂无
暂无

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

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