My delete picture code doesn't work

ashley12

New Member
Joined
Dec 6, 2020
Messages
38
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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,299
Office Version
  1. 2010
Platform
  1. Windows
it might pay to see if your pics are
VBA Code:
Pic.type=msolinkedpicture 'or
Pic.type=msoEmbeddedOLEObject
 
Solution

ashley12

New Member
Joined
Dec 6, 2020
Messages
38
Office Version
  1. 365
Platform
  1. Windows
Thank you. I've check and it says the type of Pic is "Shape" 😅
Is there a more specific property it than type?
 

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,299
Office Version
  1. 2010
Platform
  1. Windows
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.
 

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,299
Office Version
  1. 2010
Platform
  1. Windows
got there in the end LOL
 

Watch MrExcel Video

Forum statistics

Threads
1,129,793
Messages
5,638,359
Members
417,023
Latest member
Zimbo38

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
Top