Author: Oscar Cronquist Article last updated on January 16, 2020

Move a shape using VBA1

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.

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.

Move a shape using VBA name manager

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?

  1. Right-click on a worksheet tab at the bottom of the Excel window. Make sure it is the worksheet you want to use.
  2. Click on "View Code".
    remote - view code
  3. Paste code to the worksheet module.
    remote - sheet module
  4. Exit VB Editor and return to Excel.
Note, save your workbook with file extension *.xlsm (macro-enabled workbook) to keep the code attached to the workbook.

Animated image

Interactive shapes

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?

Move a shape using VBA identify left and top points

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?

Move a shape using VBA identify left and top points vba 1

  1. Press Alt + F11 to open the Visual Basic Editor.
  2. Click on "Insert" on the top menu.
  3. Click on "Module".
  4. Copy above VBA code and paste to code window.
  5. Exit VB Editor and return to Excel.

How to run the macro?

Move a shape using VBA assign macro to shape

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.

  1. Right-click on shape "Straight Arrow Connector 42".
  2. Click on "Assign macro".
  3. A dialog box appears, click on the macro you want to assign.
    Move a shape using VBA link macro to shape 1
  4. Click the OK button.

How to find out the width and height of a shape?

Move a shape using VBA shape size

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.

Download Excel file


Remote.xlsm