vba identifying object


starstarstarstarstar Excellent

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?



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


MsgBox shpShape.Name

Next shpShape



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

RE: VBA - Identifying object names


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!




Live dates & prices


Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


Excel tip:

Removing the Ribbon from view in Excel 2010

At times when you want to view the whole spreadsheet, try double clicking on the ''Home'' tab on the ribbon which will hide the ribbon from view.

View all Excel hints and tips