vba identifying object
Request a callback

We'll call during UK business hours

Name:
Number:
0207 987 3777

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » VBA - Identifying object names

VBA - Identifying object names

resolvedResolved · Low Priority · Version 2003

Tue 18 Aug 2009, 16:02 replyReply Delegate Simon said...

Simon has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course

VBA - Identifying object names

How do you find out what the ‘name’ of an object is? For example, if I draw a line (using the ‘line’ tool, or autoshape), or if I insert a chart on an existing sheet, how do I find out how to refer to the object in VBA?

I have a sort of work around for autoshapes, if I right click on the object and pretend to assign a macro, the name of the object defaults as the macro name in the ‘macro name’ field of the pop up window – eg: [Line148_Click]. It took a bit of struggle with VBA to discover that VBA sees the object as ‘Line 148’ (ie: with a space).

I can the write code such as:
ActiveSheet.Shapes("Line 148").Visible = True ‘or False

and set other properties of the object.

This is okay as far as it goes, but it does not work for all objects that can appear on a worksheet. Also, I can’t believe that there is not a more appropriate and generic option.

Any ideas?

Cheers,

S!

For upcoming training course dates see: Pricing & availability

Thu 20 Aug 2009, 15:46 replyReply Trainer Stephen said...

RE: VBA - Identifying object names

hi Simon

Thanks for your question

I am currently exploring several options, and will follow up this post with any results

In the meantime, the following snippet of code will select each shape on a worksheet and tthen display a message box with its name. You could use this to identify the name of all the shapes and make a note of its name

Dim shpShape As Shape

For Each shpShape In ActiveSheet.Shapes

shpShape.Select

MsgBox shpShape.Name


Next shpShape


Regards

Stephen

Mon 28 Sep 2009, 11:11 replyReply Delegate Simon said...

RE: VBA - Identifying object names

Stephen,

I came across some VBA to identify the name of an active chart ( MsgBox ActiveChart.Name ), which gave me an idea.

If you click on an object or form button and then run


Public Sub ObjectNameID()

MsgBox Selection.Name

End Sub

this returns the name of the object specifically selected and in a form that will work in VBA, ie: 'Line 158' or whatever.

The only down side is that if you have both form buttons and control buttons on a worksheet it is tricky to tell the difference with the naked eye and accidentally selecting a control button will most likely cause the associated code to execute!

Cheers,

S!

 

Please browse our web site to find out more about
excel training program and other Microsoft training courses.

Excel tip:

Percentage format

Ctrl+Shift+% applies the Percentage format, with no decimal places.

View all Excel hints and tips



Excel training
Course rating:
4.7 stars - based on 18023 reviews
Microsoft Certified Partner Institute of Leadership and Management - Certified Courses Learning and Performance Institute - Accredited Training Provider Security Seal verified by visa, mastercard securecard

Connect with us: Facebook · Twitter · Google+ · LinkedIn · Pinterest

2nd Floor, CA House, 1 Northey Street
Limehouse Basin, London, E14 8BT
United Kingdom