How to draw a rectangle on the specific coordination ?

sthanawa

Board Regular
Joined
Jul 5, 2006
Messages
80
Dear All ,

I have one question . I need to draw one rectangle box on the specific coordination of X and Y.

Example , Coordinate X = 100 pixels
Coordinate Y = 100 pixels

How and can I draw this box by VBA in Excel ? Please suggest...

Assume the rectangle box have some fixed side.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi,

you can record a macro for this
Code:
    ActiveSheet.Shapes.AddShape(msoShapeRectangle, 127.5, 108.75, 135#, 44.25). _
        Select
    ActiveSheet.Shapes.AddShape(msoShapeRectangle, 144#, 178.5, 104.25, 25.5). _
        Select
then check the helpfiles
and experiment
when you change some values largely, you see better what it does
Code:
    ActiveSheet.Shapes.AddShape(msoShapeRectangle, 500, 178.5, 104.25, 25.5). _
        Select
500 seems to be the "LEFT" property-value
now you can suspect the second to be the "TOP" property
so change "left" to 0 & "top" to a high number to be sure your assumption is correct
why not change width and height, to see in which order they should be put ?
Code:
    ActiveSheet.Shapes.AddShape(msoShapeRectangle, 0, 500, 5, 200). _
        Select
OK: the order is
LEFT-TOP-WIDTH-HEIGHT :)
do we need that "select"
Code:
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 100, 100, 200, 20)
hmmmm, yes ?

but don't like it
so check the helpfiles
click in AddShape and hit F1
:eek: you need to delete the argurment brackets when you're not "doing" something with the object !!
Code:
Set myDocument = Worksheets(1)
myDocument.Shapes.AddShape msoShapeRectangle, 50, 50, 100, 200

OK, getting closer :)
Oh, we solved your question already...

let's go one step further
why not allign this with a certain range ?
this is a beauty
Code:
Sub put_rectangle_in_range()

    With Range("B2:D5")
    ActiveSheet.Shapes.AddShape msoShapeRectangle, .Left, .Top, .Width, .Height
    End With

End Sub
kind regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,239
Members
448,555
Latest member
RobertJones1986

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