Solved

Excel 2013 VBA - select multiple shapes

Posted on 2015-01-15
5
4,117 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
[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
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 51

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 12

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

734 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