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
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. Download 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.
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.
Where to put the event code?
- Right-click on a worksheet tab at the bottom of the Excel window. Make sure it is the worksheet you want to use.
- Click on "View Code".
- Paste code to the worksheet module.
- Exit VB Editor and return to Excel.
Animated image
I created a small animated image that shows what happens when you click on each cell in cell range F5:F13, note that the shape is hidden if you click a cell outside cell range F5:F13.
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
Where to put the macro code?
- Press Alt + F11 to open the Visual Basic Editor.
- Click on "Insert" on the top menu.
- Click on "Module".
- Copy above VBA code and paste to code window.
- Exit VB Editor and return to Excel.
How to run the macro?
You can link assign the macro to the shape which will start the macro each time you click on shape. You can also press shortcut keys Alt + F8 to open the macro dialog box and start the macro from there.
- Right-click on shape "Straight Arrow Connector 42".
- Click on "Assign macro".
- A dialog box appears, click on the macro you want to assign.
- Click the OK button.
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.
This article demonstrates a macro that copies values between sheets. I am using the invoice template workbook. This macro copies […]
Open Excel files in a folder [VBA]
This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]
Split data across multiple sheets [VBA]
In this post I am going to show how to create a new sheet for each airplane using vba. The […]
Identify missing numbers in a column
The image above shows an array formula in cell D6 that extracts missing numbers i cell range B3:B7, the lower […]
Working with COMBO BOXES [Form Controls]
This blog post demonstrates how to create, populate and change comboboxes (form control) programmatically. Form controls are not as flexible […]
Change chart data range using a Drop Down List [VBA]
In this article I will demonstrate how to quickly change chart data range utilizing a combobox (drop-down list). The above […]
This workbook contains two worksheets, one worksheet shows a calendar and the other worksheet is used to store events. The […]
Run a Macro from a Drop Down list [VBA]
This article demonstrates how to execute a VBA macro using a Drop Down list. The Drop Down list contains two […]
What's on this page Copy a file Copy and rename a file Rename a file List files in a folder […]
How to change a picture in a worksheet dynamically [VBA]
Rahul asks: I want to know how to create a vlookup sheet, and when we enter a name in a […]
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:
Click 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) […]