Solved

Excel 2013 VBA - select multiple shapes

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

738 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