VBA add a circle


0. Quick guide - add a circle to a WS


In this module:

  1. VBA code - to add a circle element to a worksheet (WS) as an msoShapeOval object (available from the Insert > Illustrations > Shapes > Basic Shapes > Oval menu sequence). The circle concept could be included in a WS dashboard interface

1. WS circle


The demonstration circle is shown in figure 1. It has top left cell anchor B5, and element width and height of 180 points.


rgb circles
Fig 1: - the blue circle msoShapeCircle from code 1

2. VBA code for WS circle


Code 1 uses the Shapes.AddShape method with parameters by name.



Code 1: Sub xlfAddCircle1 procedure adds a circle element to the WS (see figure 1)
Option Explicit
Const topleft As String = "C5"  ' anchor cell
Const diam As Integer = 180     ' points

' ===========================
Sub xlfAddCircle1()
Dim Shp As Shape
Dim TLCleft As Double
Dim TLCtop As Double

        TLCleft = Range(topleft).Left
        TLCtop = Range(topleft).Top

        Set Shp = ActiveSheet.Shapes.AddShape(Type:=msoShapeOval, _
                                              Left:=TLCleft, Top:=TLCtop, _
                                              Width:=diam, Height:=diam)
        With Shp
                .Fill.Visible = msoFalse
                .Line.Weight = 10
                .Line.ForeColor.Brightness = 0.4
                .ThreeD.BevelTopType = msoBevelCircle
        End With

End Sub


References