Solved

Excel 2013 VBA - select multiple shapes

Posted on 2015-01-15
5
3,031 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 49

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 10

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
macro for finding text and replacing 7 43
Remove row and column 3 45
SUMPRODUCT with criteriya 5 25
Excel file not created as expected 7 48
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…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

912 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

23 Experts available now in Live!

Get 1:1 Help Now