ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Identifying Objects (https://www.excelbanter.com/excel-programming/342496-identifying-objects.html)

Jzz

Identifying Objects
 
Hi all!
I'm programming in Excel as a hobby, and I made a macro which draws
several objects. These objects represent loaded cargo on the ship I'm
sailing. When I want to change the place of these objects, I have to
start a seperate macro. I can do that by clicking on the object itself,
but how can I let the macro know on which object I'm clicking? (the
objects all have a name such as object1, object2 and so on)
Better still would it be to be able to shift the objects by mouse, but
then how does the Macro know which object I'm shifting and where to?
I hope this explanation was clear enough, and that someone has a
solution.
Greets, Jzz


Nick Hebb

Identifying Objects
 
Assuming you assigned a macro via the shapes' OnAction properties, you
could have the macro pop open a user form with a list box populated
with all the shapes. Select the shape and click OK, then continue on
with the rest of the macro.

I think the big problem is how to identify each shape. Before doing the
above, you could write a little macro to go through and give the shapes
meaningful names:

Dim shp As Shape

For Each shp In ActiveSheet.Shapes
shp.Select
shp.Name = InputBox("Enter shape name:")
Next

Set shp = Nothing

Then you could use the shapes names to populate the list box I spoke of
above.

Hopefully, someone here comes up with a better solution because the
first part seems really kludgy. But if not, I hope that helps.

Nick Hebb
BreezeTree Software
http://www.breezetree.com


Jzz

Identifying Objects
 
Hi Nick,
Thanks for your prompt reply, but this was not really what I was
looking for. The shapes all have meaningfull names, but the big point
is how to let the macro know on which of the shapes I'm clicking. The
listbox is already some advantage, but not really nessesary as all the
objects have numbers written on them.

Jzz


Andy Pope

Identifying Objects
 
Hi,

If the macro is assigned to a shape then

MsgBox Application.Caller

will display the shapes name.

Cheers
Andy

Nick Hebb wrote:
Assuming you assigned a macro via the shapes' OnAction properties, you
could have the macro pop open a user form with a list box populated
with all the shapes. Select the shape and click OK, then continue on
with the rest of the macro.

I think the big problem is how to identify each shape. Before doing the
above, you could write a little macro to go through and give the shapes
meaningful names:

Dim shp As Shape

For Each shp In ActiveSheet.Shapes
shp.Select
shp.Name = InputBox("Enter shape name:")
Next

Set shp = Nothing

Then you could use the shapes names to populate the list box I spoke of
above.

Hopefully, someone here comes up with a better solution because the
first part seems really kludgy. But if not, I hope that helps.

Nick Hebb
BreezeTree Software
http://www.breezetree.com


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info

Jzz

Identifying Objects
 
Hi,

Thanks for the reply, but this does not display the shapes name, simply
the caller of the macro. This is always "object", whether the name of
the object is 'object', 'trailer' or 'whatever' does not matter, so
this is not the solution either, sorry.

Jzz


Nick Hebb

Identifying Objects
 
Andy, that's pretty sweet. I didn't know about Application.Caller.

Jzz, this works in the OnAction macro:

Dim shp As Shape

Set shp = ActiveSheet.Shapes(Application.Caller)
MsgBox shp.Name
shp.Left = shp.Left + 100 ' just for fun
Set shp = Nothing

Nick Hebb
BreezeTree Software
http://www.breezetree.com


Jzz

Identifying Objects
 
Hi, yes I worked it out already, the problem was that the shape was
part of a group, and application.caller gives the name of the shapes
seperately; not of the group. This caused a little confusion.
Many thanks!
Jzz



All times are GMT +1. The time now is 10:31 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com