Refer to a shape by its position

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Normally one refers to a shape by its name, such as:

Code:
Dim ShapeName As String

ShapeName = Application.Caller

The problem I have is my worksheet contains two shapes with the same name.

So instead, I would like to refer to the shape that is clicked by its position, relative to the worksheet, ie what is its top left, so I can tell which row it's on.

Is this possible?

Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Their names are really the same if you look at the drop down left of the formula bar when they are selected? Would not have thought possible.
Anyway, you could distinguish between them as you've asked by using their ID value? To get that, in a standard module run this using your sheet name:
VBA Code:
Sub shapeNames()
Dim shp As Shape

For Each shp In Sheets("4").Shapes
     Debug.Print shp.Name & shp.ID
Next

End Sub
 
Upvote 0
Their names are really the same if you look at the drop down left of the formula bar when they are selected? Would not have thought possible.
Anyway, you could distinguish between them as you've asked by using their ID value? To get that, in a standard module run this using your sheet name:
VBA Code:
Sub shapeNames()
Dim shp As Shape

For Each shp In Sheets("4").Shapes
     Debug.Print shp.Name & shp.ID
Next

End Sub
Thanks for your advice.

Firstly, I too didn't think you could have two shapes on the same worksheet with the same "name".

I say "name" because I assume the value in the named range box is the name property.


Say I have a shape and the named range box shows abc.

If I add another shape (manually) and proceed to the named range box to change it to abc, the moment I click enter, the first shape is highlighted, thus confirming it's NOT possible to have two shapes with the same "name".

However, if I copied the first shape and pasted it within the worksheet, the named range box DOES show abc.


Back to your suggestion, the immediate window does show two different outputs: abc2 and ab4.

So how do I see this info on the shape itself because all the named range box shows is abc)?
 
Upvote 0
Did you run that code or not (not clear to me)? I modified to Debug.Print shp.Name & " ID:" & shp.ID and tested again to be sure of what I was looking at. I get
Rectangle 6 ID:7
Check Box 2 ID:6146
5-Point Star 2 ID:3
Rectangle 3 ID:4
Comment 1 ID:6145
I'd say it's impossible to have 2 shapes with the same ID, so I would discern them by ID. Perhaps you can verify with a new test.
If what you posted is a concatenation of name & ID without a space, they don't look like they have the same name to me: abc and ab? 2 and 4 are the ID's? Maybe try the modified code to double check their ID's.
 
Upvote 0
Did you run that code or not (not clear to me)? I modified to Debug.Print shp.Name & " ID:" & shp.ID and tested again to be sure of what I was looking at. I get
Rectangle 6 ID:7
Check Box 2 ID:6146
5-Point Star 2 ID:3
Rectangle 3 ID:4
Comment 1 ID:6145
I'd say it's impossible to have 2 shapes with the same ID, so I would discern them by ID. Perhaps you can verify with a new test.
If what you posted is a concatenation of name & ID without a space, they don't look like they have the same name to me: abc and ab? 2 and 4 are the ID's? Maybe try the modified code to double check their ID's.
I ran your original code and it did distinguish between the two shapes, one returned abc2 and the other abc4.

Running your amended code returned this:

Code:
abc ID:2
abc ID:4

My point is if the ID property was not added to your code, I wouldn't know which shape is which.
 
Upvote 0
You mentioned .Top in your post, so you ask what is the .Top of ID 2 vs the .Top of ID 4 then you'll know which is which going forward?
I suggested ID because I thought there might not be any visible differences such as caption/fill colour/border/etc. that you could test for.
You could probably rename the shape for ID 2 or 4 if that helps.
 
Upvote 0
You mentioned .Top in your post, so you ask what is the .Top of ID 2 vs the .Top of ID 4 then you'll know which is which going forward?
I suggested ID because I thought there might not be any visible differences such as caption/fill colour/border/etc. that you could test for.
You could probably rename the shape for ID 2 or 4 if that helps.
Thanks for your suggestion.
 
Upvote 0
However, if I copied the first shape and pasted it within the worksheet, the named range box DOES show abc.
Strange !
It is not possible to have two shapes with the same name in the same worksheet whether the name is entered manually or the shape is copied !!
 
Upvote 0
Strange !
It is not possible to have two shapes with the same name in the same worksheet whether the name is entered manually or the shape is copied !!
How can I share a video clip with you, which shows that somehow, I have two shapes with the same name?
 
Upvote 0

Forum statistics

Threads
1,215,339
Messages
6,124,375
Members
449,155
Latest member
ravioli44

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