Delete only selected pictures in Excel worksheet

SerenityNetworks

Board Regular
Joined
Aug 13, 2009
Messages
131
Office Version
  1. 365
Platform
  1. Windows
I'm using a VBA macro to load 15 pictures in various positions in an Excel worksheet. I'll examine those pictures, delete them, then load some more. The process repeats hundreds of times. This works fine if I want to delete all the pictures that I load. However, I'd like to add some shapes that I don't want deleted. I'd also like to add a 16th picture that I don't want deleted. I'm at a loss as to how to do this (I can manage working with VBA, but I'm not a coder.)

Right now, I'm loading each picture using the following code.

Code:
Sub Image15()
    Dim objPicture As Picture
    With Sheet4.Cells(1, 1) ' Picture displays in cell from row, column
        Set objPicture = .Parent.Pictures.Insert(Sheet4.Cells(2, 51).Value) ' Picture path row, column
        objPicture.Top = gvarImage15Top
        objPicture.Left = gvarImage15Left
        objPicture.Width = gvarImage15Width
        objPicture.name = "pic15" ' Trying to see if I can delete by name
    End With
End Sub

As you can see, I've tried naming the picture so I can delete it by name. But if this is the suggested method for deleting specific pictures then please help me with the syntax I need to do it. I've not been able to figure it out.

The following code works fine to delete all pictures, but as I mentioned, my goal is to only delete 15 of the pictures (not the 16th picture or shape I've added to the worksheet.
Code:
Sub DeleteImages()
    Dim shape As Excel.shape
    For Each shape In ActiveSheet.Shapes
        shape.Delete
   'Next
End Sub

Any help on this will be appreciated.

Thanks,
Andrew
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
If you will ever only have pictures and shapes, you could loop through the Pictures collection...

Code:
Sub DeleteFirst15Pictures()

    Dim i As Long
    
    On Error Resume Next
    For i = 1 To 15
        ActiveSheet.Pictures(i).Delete
    Next i
    On Error GoTo 0
    
End Sub

Otherwise, try...

Code:
Sub DeleteFirst15Pictures()
    Dim shape As shape
    Dim cnt As Long
    cnt = 0
    For Each shape In ActiveSheet.Shapes
        If shape.Type = msoPicture Then
            cnt = cnt + 1
            If cnt <= 15 Then
                shape.Delete
            Else
                Exit For
            End If
        End If
    Next shape
End Sub

Hope this helps!
 
Last edited:
Upvote 0
Thank you Domenic. Your second option wasn't working. So I tried the following to identify the picture type. I found it was type 11, msoLinkedPicture. I substituted msoPicture with msoLinkedPicture and it worked perfectly.

Thanks again,
Andrew

Code:
Sub IdentifyShapes()
    Dim s As shape
    For Each s In ActiveSheet.Shapes
        MsgBox s.Type & vbCrLf & s.name
    Next s
End Sub
 
Last edited:
Upvote 0
That's great. Glad you were able to make it work.

Cheers!
 
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