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.
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,367
Messages
5,528,271
Members
409,813
Latest member
robyrux

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top