VBA Excel if statement for removing shapes with ID like something

135 views Asked by At

I have more than 1 shape called "Picture..." in my active workbook.

I want to remove it along with the other shapes, which have their specified IDs~

Since there is no problem with them, because they have an unique ID, I have problem with them two, called "Picture...".

My code looks like this:

 Sub ImgFullRemove3()
 Dim Pic As Picture
 Dim PicName As String, PicName2 As String, PicName3 As String
  On Error Resume Next
  Application.ScreenUpdating = False
  PicName = "Cable_Number"
  PicName2 = "Divider"
  PicName3 = "*Picture*"

  For Each Pic In ActiveSheet.Pictures
  Debug.Print Pic.Name
    If Pic.Name = PicName Or PicName2 Or PicName3 Then
        ActiveSheet.Shapes("Cable_Number").Delete
        ActiveSheet.Shapes("Divider").Delete
        ActiveSheet.Shapes("*Picture*").Delete
    End If
  Next
  Application.ScreenUpdating = True

 End Sub

It returns nothing, similarily to the another one below:

  Sub ImgFullRemove()
  Dim Img As Picture
  Dim ImgName As String
  On Error Resume Next
  Application.ScreenUpdating = False
  For Each Img In ActiveSheet.Pictures
  Debug.Print Img.Name
  If Img.Name Like "*Picture*" Then
  ActiveSheet.Shapes.Delete
  End If
  Next
  Application.ScreenUpdating = True

  End Sub

that refers only to these "Pictures..." objects. The result is exactly the same.

How can I make this code running properly?

1

There are 1 answers

5
CLR On

Here's a modification to your first sample code that I think will achieve your desired outcome.

PicName = "Cable_Number"
PicName2 = "Divider"
PicName3 = "*Picture*"

For Each pic In ActiveSheet.Pictures
  If pic.Name = PicName Or _
      pic.Name = PicName2 Or _
      pic.Name Like PicName3 Then
          pic.Delete
  End If
Next

Note: you can't use If in the way you'd tried - i.e if A = X, Y or Z.

You have to test if A = X or A = Y or A = Z.