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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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