Waleed Asif
Waleed Asif

Reputation: 199

Coloring Circle in Vba

I am new to Vba, I have to draw circles which are color filled in the following way

1/4 color filled circle, Half color filled circle, 3/4 color filled circle

I know I can draw a color-filled circle with the following code

 Set shpOval = ActiveSheet.Shapes.AddShape(msoShapeOval, curCellLeft, curCellTop, 20, 20)
 shpOval.Fill.ForeColor.RGB = RGB(128, 0, 0)

Above code gives me a full color filled circle, which property I have to change to get circle shapes that I have mentioned above.

Upvotes: 0

Views: 1015

Answers (2)

FunThomas
FunThomas

Reputation: 29336

Just for fun: Playing around with PIE-Shapes.

Sub DrawCircle(pieces As Integer, size As Double, position As Range, color)
        
    If pieces < 1 Then pieces = 1
    If pieces > 4 Then pieces = 4
    Dim varShape() As String
    ReDim shapeNames(0 To pieces - 1)
    
    Dim i As Long
    For i = 0 To pieces - 1
        Dim sh As Shape
        Dim x As Double, y As Double
        x = position.Left + IIf(i = 1 Or i = 2, size, 0)
        y = position.Top + IIf(i >= 2, size, 0)
        
        Set sh = position.Parent.Shapes.AddShape(msoShapePieWedge, x, y, size, size)
        shapeNames(i) = sh.Name
        sh.Rotation = i * 90
        If IsArray(color) Then
            sh.Fill.ForeColor.RGB = color(i + LBound(color))
        Else
            sh.Fill.ForeColor.RGB = color
        End If
        sh.Line.Visible = False
    Next i

    If pieces > 1 Then
        position.Parent.Shapes.Range(shapeNames).Group
    End If
End Sub

Playing with it:

Sub test()
    Call DrawCircle(3, 20, ActiveCell, vbRed)
    Call DrawCircle(4, 10, ThisWorkbook.Sheets(1).Range("F3"), Array(vbYellow, vbYellow, vbBlue, vbYellow))
    Call DrawCircle(1, 40, ActiveCell.Offset(2, 2), vbGreen)
End Sub

Upvotes: 1

PerlBatch
PerlBatch

Reputation: 210

Create a range of values in excel from A1 to A4 as 25,50,75,100. Go to Insert and select "Doughnut" chart.

Upvotes: 0

Related Questions