Move a shape [VBA]
This article demonstrates how to move a shape, a black arrow in this case, however, you can use whatever shape you want, using VBA code. Select a cell containing the button name, and the black arrow instantly points to the corresponding button.
What's on this page
1. How to change the location of a shape on worksheet
I made a simple picture from an old remote I found, see image above. The macro is actually event code placed in a worksheet module, I will describe this in detail.
The VBA code checks that the selected cell is in cell range F5:F13. If true, then it reads the coordinates on the same row. The shape is then moved to that location. Get the workbook and try it out.
The arrow is hidden if the selected cell is not one of the cells in cell range F5:F13.
1.1 VBA code
'Event code is started if a cell is selected Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. With ActiveSheet.Shapes.Range(Array("Straight Arrow Connector 42")) 'Make sure the selected cell is in cell range F5:F13 If Not Intersect(Target, Range("F5:F13")) Is Nothing Then 'Make the shape visible .Visible = True 'Change the number of points between the shape and the row numbers .Left = ActiveCell.Offset(0, 1) ''Change the number of points between the shape and the column letters .Top = ActiveCell.Offset(0, 2) Else 'Hide the shape .Visible = False End If End With End Sub
Make sure you change the shape name if your shape has a different name. To check the shape name select it and read the name from the name box, see the image below.
You can find the name the top left corner "Straight Arrow Connector 42" which matches the name in the macro.
1.2 Where to put the event code?
- Press with right mouse button on on a worksheet tab at the bottom of the Excel window. Make sure it is the worksheet you want to use.
- Press with mouse on "View Code".
- Paste code to the worksheet module.
- Exit VB Editor and return to Excel.
1.3 Animated image
I created a small animated image that shows what happens when you press with left mouse button on each cell in cell range F5:F13, note that the shape is hidden if you press with left mouse button on a cell outside cell range F5:F13.
2. How to get the top and left value of a shape?
The following macro shows a message box with the left and top points of shape "Straight Arrow Connector 42".
'Name macro Sub Position() 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. With ActiveSheet.Shapes("Straight Arrow Connector 42") 'Show a message box containing top and left points MsgBox "Left: " & .Left & " Top: " & .Top End With End Sub
2.1 Where to put the macro code?
- Press Alt + F11 to open the Visual Basic Editor.
- Press with mouse on "Insert" on the top menu.
- Press with mouse on "Module".
- Copy above VBA code and paste to code window.
- Exit VB Editor and return to Excel.
2.2 How to run the macro?
You can link assign the macro to the shape which will start the macro each time you press with left mouse button on shape. You can also press shortcut keys Alt + F8 to open the macro dialog box and start the macro from there.
- Press with right mouse button on on shape "Straight Arrow Connector 42".
- Press with mouse on "Assign macro".
- A dialog box appears, press with left mouse button on the macro you want to assign.
- Press with left mouse button on the OK button.
3. How to find out the width and height of a shape?
The following macro displays a message box containing the height and width of shape "Straight Arrow Connector 42".
Sub Size() With ActiveSheet.Shapes("Straight Arrow Connector 42") MsgBox "Height: " & .Height & " Width: " & .Width End With End Sub
Put this macro in a module just like the other macro above, this is not event code.
Macro category
This article demonstrates how to add or remove a value in a regular drop down list based on a list […]
In this tutorial, I am going to show you how to add values to a drop down list programmatically in […]
This article demonstrates how to place values automatically to a table based on two conditions using a short macro. Cell […]
Excel categories
7 Responses to “Move a shape [VBA]”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
HI Oscar,
How did you decide LEFT & TOP pixel..
If I insert a Picture in Sheet.. how can I decide its Left & Top Position..
Regards,
=DEC2HEX(3563)
Debraj Roy,
Assign the following macro to your shape:
Press with left mouse button on the shape and a message box shows the coordinates.
You don't have to create a macro to get that information; simply select the shape then, with the shape still selected, go into the VB editor (ALT+F11) and execute this in the Immediate Window (press CTRL+G if you do not see it)...
Thank you for commenting!
Just wanted to point out that you can simplify this line of code...
to this...
The one thing about this that may surprise people (it was a surprise to me) is that Shapes does not require a direct reference to the worksheet object that contains it (like it does if you try to use Shapes in a General code Module). My guess at why is because, being the code is located in a [b]Sheet[/b] Module, Shapes can only be defaulting to it.
Rick Rothstein (MVP - Excel),
Thanks for sharing!
[…] Read more: Move a shape (vba) […]