Solved

Excel 2013 VBA - select multiple shapes

Posted on 2015-01-15
5
2,805 Views
Last Modified: 2015-01-16
I have a number of small rectangles on a sheet. I can iterate through the shapes that are positioned within a selected cell range ok.

My problem is this. For example, I know the syntax looks like
Sheet1.Shapes.Range(Array("Rectangle 1", "Rectangle 2", "Rectangle 3")).Select

I need to be able to construct a string "Rectangle 1", "Rectangle 2", "Rectangle 3" programmatically, right? But when I do I get an extra set of " that I cannot get rid of, and I get an error saying "selected item not found".
(I should clarify, the shapes will have different names. ie. not always the same. Hence programmatically)

So, how can I select multiple shapes please?
Thanks
0
Comment
Question by:hindersaliva
5 Comments
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 40552658
Please post your code.
0
 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 350 total points
ID: 40552760
Having your code or some more details on what you are trying to accomplish will help.  For example, do you have to select the shapes by name?  Are there more shapes on the sheet that you do not want to select?  What criteria are you using to select the shapes?  Are they always in the same order?

You can select all shapes on a sheet with:
Sheet1.Shapes.SelectAll

Open in new window


You can select in the range by the index instead:
Sheet1.Shapes.Range(Array(2, 3, 4)).Select

Open in new window


You could also maybe put the names in defined array.  Here is a sample:
Sub test()
    Dim objShape As Shape
    Dim arrShapeNames() As String
    Dim i As Integer: i = 0
    
    ReDim arrShapeNames(Sheet1.Shapes.Count)
    
    For Each objShape In Sheet1.Shapes
        arrShapeNames(i) = objShape.Name
        i = i + 1
    Next
    
    Sheet1.Shapes.Range(arrShapeNames).Select
End Sub
    

Open in new window

0
 
LVL 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 100 total points
ID: 40552898
Hi,

I suppose to create your str array you have an array that has one element more than necessary then try after creating your array

ReDim Preserve arrYourArrayShapeNames(UBound(arrYourArrayShapeNames) - 1)

you can directly use the array instead of changing it into strings

Regards
0
 
LVL 9

Assisted Solution

by:Jamie Garroch
Jamie Garroch earned 50 total points
ID: 40552999
You can loop through all shapes in the shape collection for a given sheet and select them with your required criteria like this:

' Selects all shapes on the sheet with names matching the search string
Sub SelectShapes(sSearch As String)
  Dim oShp As Shape
  For Each oShp In Sheet1.Shapes
    If LCase(oShp.Name) Like "*" & sSearch & "*" Then oShp.Select False
  Next
End Sub

Open in new window

0
 

Author Closing Comment

by:hindersaliva
ID: 40553245
Thanks. I didn't know I can use an Array of shape names. It made it a lot simpler!!!!!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now