Select all unwanted shapes in range

ceecee88

Board Regular
Joined
Jun 30, 2022
Messages
59
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi, I'm trying to select all the unwanted shapes in the range and I'm almost there but get stuck on one issue.
Example below, I'm trying to select all shapes that are aren't named Rectangle 1 and Rectangle 5 in range A1:O30 and it should select 3 shapes which are Rectangle 12 and Rectangle x 2. The problem with my code is that it won't select if the name is duplicate. Now, it is only select Rectangle 12 as Rectangle has 2 shapes with the same name. How do I adjust it to select all unwanted shapes even if it has duplicate name.

Here is my code, any suggestion will be much appreciated.
Thank you

VBA Code:
Sub SelectunwatedShape()
Dim shp As Shape, r As Range, s As Range, myarr(), n As Integer
ReDim myarr(ActiveSheet.Shapes.Count)

Set r = Range("A1:O30")
ActiveCell.Select
  
   For Each shp In ActiveSheet.Shapes
    If Not Intersect(Range(shp.TopLeftCell, shp.BottomRightCell), r) Is Nothing Then
    If shp.Name <> "Rectangle 1" And shp.Name <> "Rectangle 5" Then
         myarr(n) = shp.Name
         n = n + 1
        End If
        End If
Next shp

On Error Resume Next
ReDim Preserve myarr(n - 1)
    ActiveSheet.Shapes.Range(myarr).Select
End Sub

1683731669764.png
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Just curious, how did you manage to have two rectangles with the same name on the same sheet?
 
Upvote 0
Not a conclusive test but it only seems to work for me with a rectangle called Rectangle.
 
Upvote 0
Try this:
VBA Code:
Sub SelectunwatedShape()
Dim R As Range, shp As Shape
Set R = Range("A1:O30")
For Each shp In ActiveSheet.Shapes
    If Not Intersect(R, Range(shp.TopLeftCell, shp.BottomRightCell)) Is Nothing Then
        If shp.Name <> "Rectangle 1" And shp.Name <> "Rectangle 5" Then
            shp.Select Replace:=False
        End If
    End If
Next shp
End Sub
 
Upvote 0
Solution
Hi, I'm not really sure what do you mean?
I was referring to the copy and paste of shapes.

If I copy and paste a rectangle shape named 'Rectangle' it creates a copy with the same name.

If I copy and paste a rectangle shape named 'Rectangle 1' it creates a copy but gives it a new suffix number depending on how many shapes there are already on the sheet.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,116
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top