Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Macro that will delete only the shapes in a specified range

Posted on 2011-09-23
5
Medium Priority
?
403 Views
Last Modified: 2012-05-12
For some reason, perhaps because my network is so slow, I find that the standard macro for this takes up a lot of time. (See code below) Just wondering if there is anything new in this area. I just can't seem to give up on the idea that there is a VBA code that selects/deletes all the shapes in Range("B2:R100") and only those shapes :-)

Thanks,
John
Sub deleteShapes()
Dim shp As Shape
    For Each shp In ActiveSheet.Shapes
            If Not Intersect(shp.TopLeftCell, Range("B2:R100")) Is Nothing Then
                    shp.Delete
            End If
    Next
End Sub

Open in new window

0
Comment
Question by:gabrielPennyback
  • 2
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
kgerb earned 750 total points
ID: 36590933
Hello John,
This is worth a shot.  It may be that the actual deletion of the object is taking the time.  Try adding the names of the shapes to an array and then deleting them all at once.  Not for certain it will help but it might.

Kyle
Sub deleteShapes()
Dim shp As Shape, Count As Long
Dim arr() As String
Count = 0
For Each shp In ActiveSheet.Shapes
    If Not Intersect(shp.TopLeftCell, Range("B2:R100")) Is Nothing Then
        ReDim Preserve arr(Count + 1)
        arr(Count) = shp.Name
        Count = Count + 1
    End If
Next
ActiveSheet.Shapes.Range(arr).Delete
End Sub

Open in new window

0
 
LVL 7

Assisted Solution

by:hippohood
hippohood earned 750 total points
ID: 36591349
it will work quicker if instead of deleting tem one by one you do the following:
- move the shapes to an unsued range (say, column AZ), instead of deleting them
- then delete the column AZ altogehter

Something like code below
Sub deleteShapes()
dim theLeft as long
theLeft = Columns("AZ:AZ").Left
Dim shp As Shape
    For Each shp In ActiveSheet.Shapes
            If Not Intersect(shp.TopLeftCell, Range("B2:R100")) Is Nothing Then
                    shp.Left=theLeft
            End If
    Next
    
Columns("AZ:AZ").Delete Shift:=xlToLeft

End Sub

Open in new window

0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 36713716
Hi kqerb, I got an error on the last line but thanks for posting. Hippohood, I can see some definite = value in your code but in my experience when I delete columns or rows, the shapes in them don'e get deleted.  

I guess there's still no way to directly select only the shapes in a specified arrange. Oh well, thanks for your suggestions. :-)

Thanks,
John
0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
ID: 36713773
Thanks again.

- John
0
 
LVL 12

Expert Comment

by:kgerb
ID: 36716750
John, after 2000 questions you should know the drill.  Please give us the opportunity to "earn" an A grade before closing the question with a lower grade.

What exactly was wrong with the code?  You got an error on line 12?  Was the sheet which you are deleting all the shapes on active when you ran the code?

Try the following, I changed Activesheet to a specific sheet.  Replace "Sheet1" with the name of your sheet.

Kyle
Sub deleteShapes()
Dim shp As Shape, Count As Long
Dim arr() As String
Dim sSheet As String

Count = 0
sSheet = "Sheet1"

For Each shp In Sheets(sSheet).Shapes
    If Not Intersect(shp.TopLeftCell, Range("B2:R100")) Is Nothing Then
        ReDim Preserve arr(Count + 1)
        arr(Count) = shp.Name
        Count = Count + 1
    End If
Next
Sheets(sSheet).Shapes.Range(arr).Delete
End Sub

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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…
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 will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

886 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