My delete picture code doesn't work

ashley12

New Member
Joined
Dec 6, 2020
Messages
43
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I used some codes to insert a picture in a sheet. Then later I want to delete it, but leave the command button. I tried 3 different codes but none of them works.

This one doesn't do anything
VBA Code:
Dim Pic As Shape
        For Each Pic In ActiveSheet.Shapes
            If Pic.Type = msoPicture Then
            Pic.Delete
            End If
        Next

This one says "Type mismatch"
VBA Code:
Dim Pic As Picture
        For Each Pic In ActiveSheet.Pictures
            Pic.Delete
        Next


This one delete both the picture and my command button
VBA Code:
Activesheet.Pictures.Delete


And here is my code to add the picture in if that helps.
VBA Code:
Sub Present_Pic()
    If PicPath = "" Then Exit Sub
  
    Dim Pic As Picture, ImageCell As Range

    Set ImageCell = Interface.Cells(20, 8)

    Set Pic = Interface.Pictures.Insert(PicPath)
    With Pic
        .Left = ImageCell.Left
        .Top = ImageCell.Top
        .Width = 400

    End With
  
    Set Pic = Nothing

End Sub

Thank you all in advance
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
it might pay to see if your pics are
VBA Code:
Pic.type=msolinkedpicture 'or
Pic.type=msoEmbeddedOLEObject
 
Upvote 0
Solution
Thank you. I've check and it says the type of Pic is "Shape" ?
Is there a more specific property it than type?
 
Upvote 0
check Shape.Name
or if there are no other shapes, you can delete them all, but considering when you trued that it errored there must be some others.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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