Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Determining the order of shape selection in a Delete Shapes macro

Posted on 2011-09-06
6
Medium Priority
?
332 Views
Last Modified: 2012-05-12
I find that the macro below never starts with the uppermost left shape, but rather goes through every other shape in the active sheet BEFORE targeting the ones I want to delete. How do I get it to start with the left uppermost shape in the specified range and/or at least ignore any shape outside of the range?

Thanks,
John


Sub DeleteSomeShapes()
Dim shp As Shape, top As Range, btm As Range
Set top = [B2]
Set btm = Cells([A4] - 7, 18)
For Each shp In ActiveSheet.Shapes
    If Not Intersect(shp.TopLeftCell, Range(top, btm)) Is Nothing Then
    shp.Delete
    End If
Next shp
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
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 36491923
Assuming A4 has the right number in it, if I test that code it only deletes shapes in the specified range.
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 36492383
Hi Stephen, yes it does that for me too. It's just that I always have to run it on a bunch of sheets and it does slow things down a little, and every once in a while it bugs for some reason and testing it is annoyingly time-consuming.

Is there anyway to keep this kind of a macro from looking at all the extraneous shapes. What would be ideal would be something that says in effect: ActiveSheet.Range("B2:R100").DrawingObjects.Delete

Thanks,
John
0
 
LVL 81

Accepted Solution

by:
byundt earned 2000 total points
ID: 36492505
John,
Assume that you have listened once again to my sermon on not selecting worksheets. Assume also that you have turned screen updating off. I believe that your code would run much faster in Excel 2003 as a result.

I rewrote your sub to have a worksheet parameter passed to it. This eliminates the need for it to be working on the active sheet. I then added a calling sub using a Select case

Sub DeleteSomeShapes(ws As Worksheet)
Dim shp As Shape, top As Range, btm As Range, rg As Range
With ws
    Set top = .[B2]
    Set btm = .Cells(.[A4] - 7, 18)
    Set rg = Range(top, btm)
    For Each shp In .Shapes
        If Not Intersect(shp.TopLeftCell, rg) Is Nothing Then
            shp.Delete
        End If
    Next shp
End With
End Sub

Sub CallingCode()
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
    Select Case LCase(ws.Name)
    Case "sheet1", "sheet3" 'Do nothing with these worksheets. Note that names should be lower case.
    Case Else
        DeleteSomeShapes ws
    End Select
Next
End Sub

Open in new window


Brad
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:gabrielPennyback
ID: 36492596
Hi Brad, thanks. Rest assured I'm not selecting any sheets! At the moment I only need to delete shapes from the active sheet, but thank you for the additional code.

I'm probably making a mountain out of a mole hill, but for some reason this macro bugged this morning and I always test codes like this with cel.Select or shp.select so that I can see the steps it's going through. Whenever I have to do that, it just annoys me that this macro invariably selects all the shapes outside the desired range before getting to the ones that matter.

As soon as I can get back into my network drive I'll test this out to see if there's some tweak in your first code that will ignore the out of bounds shapes. If not, then I will do everything in my power to get out of denial and simply let mole hills be mole hills :-)

Thanks,
John
0
 
LVL 81

Assisted Solution

by:byundt
byundt earned 2000 total points
ID: 36492743
John,
I was reacting to your statement "It's just that I always have to run it on a bunch of sheets and it does slow things down."

If you are only deleting shapes from the active sheet, then make sure you turn screen updating off before running the code. In a test deleting 281 shapes from a region on a worksheet, it made a 40% reduction in the time required.

If you ever might be deleting shapes from a non-active worksheet, then please use the sub I suggested with the worksheet parameter. It will work just as well when called like either of:
DeleteSomeShapes ActiveSheet
DeleteSomeShapes Worksheets("Sheet4")

Was the worksheet protected that had the problem with the code this morning? You need to unprotect it first.

Was cell A4 empty? It needs to have a value of 9 or more to be consistent with your code. You might change the statement setting range variable btm to:
    Set btm = .Cells(Application.Max(2, Val(.[A4]) - 7), 18)          'Works even if A4 is text or a number less than 9

If you knew the names of the shapes in the range of interest, you could delete just the names drawn from that list. One way of knowing the names is to set the name after you create the shape:
Dim shp As Shape
Set shp = Activesheet.Shapes(Activesheet.Shapes.Count)
shp.Name = "Rectangle" & shp.TopLeftCell.Address(False, False)      'Name it like RectangleB8

Brad
0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
ID: 36498671
Great analysis and a wealth of useful info, thanks Brad!

- John
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

618 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