Moving & positioning shapes on sheet by selecting cell destination

benntw

Board Regular
Joined
Feb 17, 2014
Messages
216
Office Version
  1. 365
Platform
  1. Windows
Not sure if there is a way to do this without a touch screen panel and programming. I have a large spread sheet that gets displayed on a screen in our dispatch. Currently I grab a round rectangle shape with the equip id on it and drag it under appropriate job. Job titles change all the time. Once I place it I try and center it the best I can. I have around 100 shapes on the excel sheet to drag around. What I am hoping to do is select any of the shapes , one at a time, and then click the cell I want it to be placed at and have instantly move & center within the cell. Is this possible to even do with excel ? Thanks for your time.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,067
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I don't think you can do this exactly the way you described, because the shape will be deselected when you click on a cell. Here's an alternative you can try. It requires that you select the shape, then run the macro below which will ask you to enter a cell address to align the shape to.

You can assign the macro to a button on your sheet which can be clicked after the shape is selected. Note that the macro is set to make the shape size equal the cell size, but you can change that if desired to any width and height you want.

Code:
Sub MoveShape()
Dim shp As String, Adr As String
If TypeName(Selection) = "Rectangle" Then
    shp = Selection.Name
    Adr = InputBox("Enter cell address to place shape")
    If Adr = "" Then Exit Sub
    With ActiveSheet.Shapes(shp)
        .Top = Range(Adr).Top
        .Left = Range(Adr).Left
        .Width = Range(Adr).Width
        .Height = Range(Adr).Height
    End With
End If
End Sub
 
Upvote 0

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try this:-
First place this code into a new BasicModule
Code:
[COLOR=black][FONT=Calibri]Option Explicit[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Public Ob As Object[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Sub oMove()[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]Set Ob =ActiveSheet.Shapes(Application.Caller)[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]End Sub[/FONT][/COLOR]

Then place this code into the Sheet Module where your shapes are.
Code:
[COLOR=black][FONT=Calibri]Private SubWorksheet_SelectionChange(ByVal Target As Range)[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]If Not Ob Is Nothing Then[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]    Ob.Left = Target.Left - Ob.Width / 2 + Target.Width / 2[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]    Ob.Top = Target.Top - Ob.Height / 2 + Target.Height / 2[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]    Set Ob = Nothing[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]End If[/FONT][/COLOR]
[COLOR=black][FONT=Calibri]End Sub[/FONT][/COLOR]


You will need to then right click each shape, Select "Assign Macro ".
Macro dialog box appears.
Look down the list of Macros, when you see the code called "oMove", click it and it then Click"OK" the code name will move to the Window at the top of the Box.
Do this for each shape.
Now when you click a Shape then Click a cell the Shape will move over the Cell.
 
Last edited:
Upvote 0

benntw

Board Regular
Joined
Feb 17, 2014
Messages
216
Office Version
  1. 365
Platform
  1. Windows
When I placed this code in my sheet the Private SubWorksheet turned red. The debug said Compile Error: Expected: expression. It was highlighting " ByVal"
 
Upvote 0

benntw

Board Regular
Joined
Feb 17, 2014
Messages
216
Office Version
  1. 365
Platform
  1. Windows
MickG

That worked excellent. I really appreciate the help. I am still a rookie at VBA. Can I ask you for one more favor ? I tried putting a msg box with and failed really bad. It would move the object on " OK " and not move it on "Cancel" just fine. The problem I had was even after I clicked on another cell the message box kept coming up. With your example file it was awesome that it moved around. Then I though if I doesn't realize I selected one of the objects I could move it by mistake.

Again I really appreciate the help you have given me.
 
Upvote 0

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try replacing the worksheet code with the below:-
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Ob Is Nothing Then
    If MsgBox("Move Shape ???", vbOKCancel + vbQuestion, "Accept/Reject") = vbOK Then
        Ob.Left = Target.Left - Ob.Width / 2 + Target.Width / 2
        Ob.Top = Target.Top - Ob.Height / 2 + Target.Height / 2
    End If
    Set Ob = Nothing
End If
End Sub
 
Upvote 0

benntw

Board Regular
Joined
Feb 17, 2014
Messages
216
Office Version
  1. 365
Platform
  1. Windows
Now I see what I did wrong. Everything works perfect. Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,190,629
Messages
5,982,020
Members
439,750
Latest member
megaman777

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top