Deleting all pictures in a range except for specified ones and find/replace question

Moonbeam111

Board Regular
Joined
Sep 24, 2018
Messages
73
Office Version
  1. 365
  2. 2010
I'm trying to delete pictures in range J22:J25 except for the picture named "ZC_Pic". Here's the code I was using. It works but it deletes ZC_Pic.

VBA Code:
Set xRg = Range("J22:J25")
    For Each xPic In ActiveSheet.Pictures
        Set xPicRg = Range(xPic.TopLeftCell.Address & ":" & xPic.BottomRightCell.Address)
        If Not Intersect(xRg, xPicRg) Is Nothing Then xPic.Delete
    Next

If I change my code to this:

VBA Code:
Dim xpic As Picture
Set xRg = Range("J22:J25")
For Each xpic In xRg
If xpic <> "ZC_Pic" Then
xpic.Delete
End If
Next xpic


I get a type mismatch error and/or application, object defined error here: For Each xpic In xRg . I don't quite understand why.


I have another question. Is there a quick and dirty way to insert a column and then use the find/replace in VBA to make every range reference one column further? For example, if my ranges were all in B and C and D. And I inserted a column, could I use a find/replace (possibly using pattern searching) to have each macro that was B,C,D now reference C,D,E?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try:
VBA Code:
Dim xPic As Picture
Set xRg = Range("J22:J25")
    For Each xPic In ActiveSheet.Pictures
        If xPic.Name <> "ZC_Pic" Then
            Set xPicRg = Range(xPic.TopLeftCell.Address & ":" & xPic.BottomRightCell.Address)
            If Not Intersect(xRg, xPicRg) Is Nothing Then xPic.Delete
        End If
    Next xPic
Your "other question" would be more appropriate for a separate thread.
 
Upvote 0
If your top code works, using "Select Case" would be one of the many possible solutions.

I guess you don't need to. JoeMo has the perfect solution.
 
Upvote 0
Thank you for the code I don't know why but it still gave me a type mismatch error pointing to the "next xpic" line however i was able to resolve it with a small modification:

VBA Code:
Set xRg = Range("J22:J25")
  For Each xpic In ActiveSheet.Shapes
    If xpic.Name Like "Picture *" Then
      Set xPicRg = Range(xpic.TopLeftCell.Address & ":" & xpic.BottomRightCell.Address)
      If Not Intersect(xRg, xPicRg) Is Nothing Then xpic.Delete
    End If
  Next xpic

Your "other question" would be more appropriate for a separate thread.

Would this sub-forum be the appropriate place for the other question? It seems like it might be more of a regular expression question, rather than a vba/excel one.
 
Upvote 0
Thank you for the code I don't know why but it still gave me a type mismatch error pointing to the "next xpic" line however i was able to resolve it with a small modification:

VBA Code:
Set xRg = Range("J22:J25")
  For Each xpic In ActiveSheet.Shapes
    If xpic.Name Like "Picture *" Then
      Set xPicRg = Range(xpic.TopLeftCell.Address & ":" & xpic.BottomRightCell.Address)
      If Not Intersect(xRg, xPicRg) Is Nothing Then xpic.Delete
    End If
  Next xpic



Would this sub-forum be the appropriate place for the other question? It seems like it might be more of a regular expression question, rather than a vba/excel one.
Did you dim xPic as Picture as shown in Post #2? If not, that may be why you received a type mismatch error.

This forum would be the right place for your other question.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
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