Solved

Excel 2010: Selecting shapes by name not id when names are numbers

Posted on 2014-09-13
6
375 Views
Last Modified: 2014-09-16
My client has a worksheet with a list shape names in column A. The rest of the worksheet has varying shapes that match the names in column A. The problem is that all of his shape names are numbers from 1 to 1000. This means when I use something like: "activesheet.shapes("128").select" the shape with the id of 128 will be selected instead of the shape with the name of "128". Is there a way to select a shape based on the name only?

Below is a small snippet of code that will select each shape that exists in column A. Any way to adjust this to select shapes by the name instead of the id?

x=1
Do

activesheet.shapes(activesheet.cells(x,1)).select

x=x+1
Loop while (x<250)
0
Comment
Question by:ouestque
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40320881
That does not happen when I do it.

I created several shapes and then named them with out of order numbers. The when I did this

ActiveSheet.Shapes("2").Select

Open in new window


followed by this

ActiveSheet.Shapes(2).Select

Open in new window


different shapes were selected.
0
 
LVL 48

Accepted Solution

by:
Martin Liss earned 334 total points
ID: 40320886
Oh, I see! Do this instead.

activesheet.shapes(activesheet.cells(x,1).Text).select
0
 
LVL 48

Assisted Solution

by:Martin Liss
Martin Liss earned 334 total points
ID: 40320890
You also might consider changing your loop to this


For x = 1 To 250

activesheet.shapes(activesheet.cells(x,1).Text).select

Next

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Assisted Solution

by:Glenn Ray
Glenn Ray earned 166 total points
ID: 40321080
As an alternate method, you could consider using the For...Each method to select all shapes in a sheet like so:
Sub Edit_Shapes()
    Dim shp As Shape
    For Each shp In ActiveSheet.Shapes
        'Test or process the shape object
        'Use shp. to control the shape object for example
        'MsgBox shp.Name
        Next shp
End Sub

Open in new window


This way, you don't have to know the names of each shape, nor do you need to know how many there are on each sheet.

Regards,
-Glenn
0
 

Author Closing Comment

by:ouestque
ID: 40325208
Awesome. Thanks!!!
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40325382
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question