vba identifying object

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

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

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!

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

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!

 

Training courses

 

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:

Colouring cells containing formulas

Cells in a worksheet can contain values or they can contain formulas. You may wish to identify all the cells in your worksheet that contain formulas by colouring those cells.

Follow these steps:
1. Choose Edit > Go To menu, or press either F5 or Ctrl+G. Excel displays the Go To dialog box.
2. Click Special. Excel displays the Go To Special dialog box.
3. Select the Formulas radio button option.
4. Select OK.

At this point, every formula cell in the worksheet is selected, and those cells can be coloured formatted as desired.

View all Excel hints and tips


Server loaded in 0.07 secs.