Identify Shape Name by it's location

Plotweaver

New Member
Joined
Jan 28, 2018
Messages
23
I need to identify a shape's name by knowing only its top and left location data.

It is obviously possible to find a selected shape's location and name.: (Example 1: find shapes.)

Dim activeshape As Shape, UserSelection As Variant
Set UserSelection = ActiveWindow.Selection
On Error GoTo NoShapeSelected
Set activeshape = ActiveSheet.Shapes(UserSelection.Name)
On Error Resume Next
MsgBox "You have selected:" & activeshape.Name & "; Top: " & activeshape.top & "; Left: " & activeshape.left
Exit Sub
NoShapeSelected:
MsgBox "You do not have a shape selected!"


But is there a way to find the name of a shape with only its location known?: (Example 2: find shape name)

Set shp = Application.ActiveSheet.Shapes(name)
shp.top = 84
shp.left = 114
Range("A1") = name


Thank you for any information you can offer to resolve this.
 
I would use a function like this :

In a Standard Module:
VBA Code:
Public Function ShapeFromLocation( _
        ByVal X As Single, _
        ByVal Y As Single, _
        Optional ByVal PerentSheet As Worksheet _
    ) As Shape
  
    Dim Shp As Shape
  
    If PerentSheet Is Nothing Then Set PerentSheet = ActiveSheet
  
    For Each Shp In PerentSheet.Shapes
        If Shp.Left = X And Shp.Top = Y Then
            Set ShapeFromLocation = Shp
            Exit For
        End If
    Next Shp

End Function


And you call the function like this :

VBA Code:
Sub Test()

    Dim Shp As Shape
  
    Set Shp = ShapeFromLocation(X:=114, Y:=84, PerentSheet:=ActiveSheet)
    If Not Shp Is Nothing Then
        MsgBox "Your shape is : '" & Shp.Name & "'"
    Else
        MsgBox "No shape found!"
    End If

End Sub
This should do the trick. While I was trying to avoid the 'if then' method, I guess going through each shape is the only way to 'find' it. Thanks.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,214,951
Messages
6,122,442
Members
449,083
Latest member
Ava19

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
Back
Top