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. 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.
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?
- 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.
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.
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.
- 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.
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.
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 […]
This tutorial shows you how to add a record to a particular worksheet based on a condition, the image above […]
This article demonstrates how to automatically create drop-down lists if adjacent data grows, there are two methods explained here. The […]
Excel does not resize columns as you type by default as the image above demonstrates. You can easily resize all […]
This article demonstrates how to automatically enter data in cells if an adjacent cell is populated using VBA code. In […]
In this small tutorial, I am going to show you how to create basic data entry with a small amount […]
Here is my contribution to all excel calendars out there. My calendar is created in Excel 2007 and uses both […]
In this article I will demonstrate how to quickly change chart data range utilizing a combobox (drop-down list). The above […]
The image above shows a chart populated with data from an Excel defined Table. The worksheet contains event code that […]
What's on this page Press with left mouse button on a specific cell to hide/show entire column Where to put […]
Question: I have multiple worksheets in a workbook. Each worksheets is project specific. Each worksheet contains almost identical format. The […]
In this blog post, I will demonstrate some VBA copying techniques that may be useful if you don't know the […]
I will in this article demonstrate a macro that automatically opens all workbooks in a folder and subfolders, one by […]
I will in this article demonstrate a macro that copies criteria from one Excel Table and applies them to another […]
This article demonstrates several VBA macros, they will save you time if you have lots of worksheets. The first macro […]
I will in this article demonstrate a macro that counts how many times a specific text string is found in […]
This article describes how to create a button and place it on an Excel worksheet then assign a macro to […]
Question: hi all, thanks for the great formula/array formula. it works great. lately, i noticed that the array formula will […]
It can sometimes be helpful having a large cell value in a comment. You can then easily hover over cell […]
This article demonstrates a macro that inserts new worksheets based on names in a cell range. The cell range may […]
Save links to your favorite macros in a personal tab on the ribbon for easy access and become more productive. […]
In a previos post:Excel vba: Save invoice data we added/copied data between sheets. This post describes how to overwrite existing […]
This workbook contains two worksheets, one worksheet shows a calendar and the other worksheet is used to store events. The […]
In this article, I am going to demonstrate a simple workbook where you can create or delete projects and add […]
This article demonstrates a macro that returns cell references for cell ranges populated with values on a worksheet. Jinesh asks: […]
In this post I am going to demonstrate how to quickly apply a filter to a table. I am using […]
In this tutorial, I am going to demonstrate how to filter an Excel define Table through a VBA macro. How it […]
This article describes different ways to locate literal or hardcoded values in formulas. The image above shows the result from […]
This post Find the longest/smallest consecutive sequence of a value has a few really big array formulas. Today I would like to […]
This article describes a macro that hides specific columns automatically based on values in two given cells. I am also […]
This article demonstrates techniques to hide and unhide worksheets programmatically. The image above shows the Excel window and the worksheet […]
This article demonstrates event code combined with Conditional Formatting that highlights overlapping date ranges based on the selected date range. […]
This post describes how to add a new custom-built item to the shortcut menu in Excel, when you press with right […]
The Quick Access Toolbar is located at the very top of your Excel window, I highly recommend that you place your […]
Rahul asks: I want to know how to create a vlookup sheet, and when we enter a name in a […]
This article demonstrates a formula and a VBA macro that returns every n-th row from a given cell range. The […]
The image above demonstrates a macro linked to a button. Press with left mouse button on the button and the […]
If you try to copy multiple cell ranges on a worksheet that don't have the same number of rows or […]
Did you know that you can select all cells containing comments in the current sheet? Press F5, press with left […]
This article describes how to create an interactive chart, the user may press with left mouse button on a button […]
Today I would like to share with you these small event handler procedures that make it easier for you to […]
This article demonstrates how to automatically create log entries when a workbook opens or closes using event code. Column A […]
In this smaller example, column D (Category) has empty cells, shown in the picture above. If your column contains thousands of […]
Macros and custom functions are great, they can automate many tedious tasks. To have them available whenever you need them, […]
This article demonstrates macros that save worksheets to a single pdf file. What's on this webpage Export all worksheets in […]
A dialog box is an excellent alternative to a userform, they are built-in to VBA and can save you time […]
This article demonstrates how to insert and use a scroll bar (Form Control) in Excel. It allows the user to […]
The image above shows an array formula in cell D6 that extracts missing numbers i cell range B3:B7, the lower […]
In this post, I am going to demonstrate how to automatically create a new sheet in the current workbook and […]
The following macro inserts a new sheet to your workbook and lists all Excel defined Tables and corresponding Table headers […]
This article demonstrates how to locate a shape in Excel programmatically based on the value stored in the shape. The […]
This article demonstrates a User Defined Function (UDF) that multiplies numbers in each row with the remaining rows in a […]
To be able to use a Pivot Table the source data you have must be arranged in way that a […]
This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]
This article explains how to set up a workbook so a macro is run every time you open the workbook. […]
In this tutorial I am going to explain how to: Create a combo box (form control) Filter unique values and […]
In this post I am going to demonstrate two things: How to populate a combobox based on column headers from […]
Excel defined Tables, introduced in Excel 2007, sort, filter and organize data any way you like. You can also format […]
This post demonstrates how to: Insert a button to your worksheet Assign a macro to the button Create a basic […]
This article demonstrates a macro that allows you to rearrange and distribute concatenated values across multiple rows in order to […]
In this post I am going to rearrange values from a list into unique columns. Before: After: The code Get […]
This article demonstrates how to run a VBA macro using a Drop Down list. The Drop Down list contains two […]
This article demonstrates a macro that copies values between sheets. I am using the invoice template workbook. This macro copies […]
This article demonstrates a macro that automatically applies a filter to an Excel defined Table based on the result from […]
This post demonstrates how to view saved invoices based on the invoice number using a userform. The userform appears when the […]
This post demonstrates a macro that automatically selects cell A1 on each sheet right before you close a workbook. The […]
This article explains how to hide a specific image in Excel using a shape as a button. If the user […]
This article demonstrates a macro and a formula that allows you to sort delimited data in a cell or cell […]
This article demonstrates how to sort a specific column in an Excel defined Table based on event code. The event […]
In this post I am going to show how to create a new sheet for each airplane using vba. The […]
This article demonstrates how the user can run a macro by press with left mouse button oning on a button, […]
This blog post describes how to insert qualifers to make "text to columns" conversion easier. Example I copied a table from […]
This blog post demonstrates how to create, populate and change comboboxes (form control) programmatically. Form controls are not as flexible […]
What's on this page Copy a file Copy and rename a file Rename a file List files in a folder […]
There are two different kinds of text boxes, Form controls and ActiveX Controls. Form controls can only be used on […]
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) […]