Solved

Macro that will delete only the shapes in a specified range

Posted on 2011-09-23
5
385 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
[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
  • 2
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
kgerb earned 250 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 250 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

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