VBA Commands to Delete All Pictures Above a Certain Row

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I received some nice code from Alex Blakenburg that allows me to delete all shapes above row 5 in my workbook. I now also want to delete all pictures above row 5 as well. So I took the original code for shapes and tried to do the same for pictures using a variable I named pic. My code is shown below.

However, the code creates a Type Mismatch error when it comes to the line - For Each pic In ws.Pictures

I would appreciate any help in correcting the code.

Thanks



VBA Code:
Private Sub Delete_Shapes_and_Pictures()


Dim ws As Worksheet
Dim shp As Shape
Dim pic As Picture


For Each ws In ThisWorkbook.Worksheets
 
    For Each shp In ws.Shapes
        If shp.Type = msoAutoShape And shp.BottomRightCell.Row < 5 Then shp.Delete
    Next shp

    For Each pic In ws.Pictures
        If pic.Type = msoPicture And pic.BottomRightCell.Row < 5 Then pic.Delete
    Next pic

Next ws
    

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Change the 'pic' part of your macro to this:
VBA Code:
'...
For Each pic In ws.Pictures
    If pic.BottomRightCell.Row < 5 Then pic.Delete
Next pic
'...
 
Upvote 0
Change the 'pic' part of your macro to this:
VBA Code:
'...
For Each pic In ws.Pictures
    If pic.BottomRightCell.Row < 5 Then pic.Delete
Next pic
'...
Thanks! That works great.

I do have a related question that I wonder I can get help on: I have grouped two shapes together and would also like them to be subject to deletion if above row 5.

When I run the corrected macro above this group is not deleted, I assume because the group is no longer a shape object. Could someone give the additional code required to delete the groups as well?

And also, out if interest, if I were to group a picture with a shape, would the same code delete that as well, or is their a distinction between a group that consists of only shapes versus one that is a mix of shapes and pictures?

Thanks again.
 
Upvote 0
When grouping two shapes together, the grouped shape becomes part of the Shapes collection. You can test for it using If shp.Type = msoGroup . . .. The same thing applies when you group a shape and a picture.
 
Upvote 0
When grouping two shapes together, the grouped shape becomes part of the Shapes collection. You can test for it using If shp.Type = msoGroup . . .. The same thing applies when you group a shape and a picture.
Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

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