Solved

Excel 2013 VBA - select multiple shapes

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

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 11

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

680 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