首页 > 解决方案 > 你怎么能画一个度数相等的“馅饼”?

问题描述

你怎么能画一个度数相等的“馅饼”?

此代码在当前工作表中创建弧线——

Sub Create_arcs()

Dim MyShape As Shape

'Your number of parts in the circle
NumParts = 6

For i = 1 To NumParts

    Set MyShape = ActiveSheet.Shapes.AddShape(msoShapeBlockArc, 0.75, 0.75, 146.25, 146.25)
    With MyShape

        .Adjustments.Item(1) = i * 360 / NumParts
        .Adjustments.Item(2) = i * 360 / NumParts + 360 / NumParts
        'Size of internal doughnut
        '.Adjustments.Item(3) = .2

        'Format for each sharpe
        .Fill.ForeColor.RGB = RGB(i * 20, i * 20, i * 20)
        .Line.ForeColor.RGB = RGB(255, 255, 255)
        .Line.Weight = 3

    End With

Next i

End Sub

这适用于 Excel,并且可以调整为适用于 PowerPoint,我猜是 Word。

接受有关如何更有效地执行此操作的建议。

标签: excelvba

解决方案


或者也许 - 多一点思考和适当的缩进:

Option Explicit ' <-- always remember this at the top of modules
Sub Create_arcs(numParts as Long)
Dim iterator as Long

    For iterator = 1 To numParts
        With ActiveSheet.Shapes.AddShape(msoShapeBlockArc, 0.75, 0.75, 146.25, 146.25)
            .Adjustments.Item(1) = iterator * 360 / NumParts
            .Adjustments.Item(2) = iterator * 360 / NumParts + 360 / NumParts
            'Size of internal doughnut
            '.Adjustments.Item(3) = .2

            'Format for each shape
            .Fill.ForeColor.RGB = RGB(iterator * 20, iterator * 20, iterator * 20)
            .Line.ForeColor.RGB = RGB(255, 255, 255)
            .Line.Weight = 3
        End With
    Next iterator
End Sub

当然,我还没有测试过——甚至上面的代码也很容易出错——如果你想要 13 个部分怎么办(提示:考虑哪些代码依赖于部分的数量)?什么是项目 1、2 和 3?也许一些简单的英语会有所帮助。

那么,你怎么能画一个度数相等的“馅饼”呢?

可能是通过设置具有所需数据量的饼图并让本机函数处理问题。


推荐阅读