VBA AddShape

3rRiXzN0O

New Member
Joined
May 5, 2011
Messages
8
I'm having trouble inserting shapes. I just want to insert a circle centered on a dynamic cell reference. I can get VBA to reference the cells I want, say, J59 and U27, but how do I get from the cell reference to the Top/Left in Points/Characters that the AddShape takes?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Welcome to the board.

Code:
Sub test()
    CircleRange Range("J59:U27")
End Sub
 
Sub CircleRange(r As Range)
    Dim fRad        As Single
    Dim fCtrX       As Single
    Dim fCtrY       As Single
 
    With r.Areas(1)
        fCtrX = .Left + .Width / 2!
        fCtrY = .Top + .Height / 2!
        fRad = Sqr((fCtrX - .Left) ^ 2! + (fCtrY - .Top) ^ 2!)
        .Worksheet.Shapes.AddShape Type:=msoShapeOval, _
                                   Left:=fCtrX - fRad, _
                                   Top:=fCtrY - fRad, _
                                   Width:=2 * fRad, Height:=2 * fRad
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,741
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top