Code to delete some images

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,375
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code to delete some images on my spreadsheet, but there are now 2 images I don't want the code to delete. The names of them are Image1 and Image2.
How can I change it so that it won't delete those images?

Code:
Private Sub cmdNoSig_Click()
ActiveSheet.Unprotect Password:="npssadmin"
Dim Pic As Object
     For Each Pic In ActiveSheet.Pictures
        If Not Intersect(Pic.TopLeftCell, Range("A12:A300")) Is Nothing Then
            Pic.Delete
        End If
    Next Pic
'ActiveSheet.Protect Password:="npssadmin"
End Sub

Thanks,
Dave
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Maybe something like

Code:
Private Sub cmdNoSig_Click()
ActiveSheet.Unprotect Password:="npssadmin"
Dim Pic As Object
For Each Pic In ActiveSheet.Pictures
    If Pic.Name <> "Image1" And Pic.Name <> "Image2" Then
          If Not Intersect(Pic.TopLeftCell, Range("A12:A300")) Is Nothing Then
              Pic.Delete
          End If
    End If
Next Pic
'ActiveSheet.Protect Password:="npssadmin"
End Sub
 
Upvote 0
Maybe something like

Code:
Private Sub cmdNoSig_Click()
ActiveSheet.Unprotect Password:="npssadmin"
Dim Pic As Object
For Each Pic In ActiveSheet.Pictures
    If Pic.Name <> "Image1" And Pic.Name <> "Image2" Then
          If Not Intersect(Pic.TopLeftCell, Range("A12:A300")) Is Nothing Then
              Pic.Delete
          End If
    End If
Next Pic
'ActiveSheet.Protect Password:="npssadmin"
End Sub

That didn't work Michael but it gave me the format of the code to work with.

This is the code that I got it to work with
Code:
Private Sub cmdNoSig_Click()
ActiveSheet.Unprotect Password:="npssadmin"
Dim Pic As Object
For Each Pic In ActiveSheet.Pictures
    If Pic.Name = "Image1" Or Pic.Name = "Image2" Then
          'If Not Intersect(Pic.TopLeftCell, Range("A12:A300")) Is Nothing Then
              Pic.Delete
          'End If
    End If
Next Pic
'ActiveSheet.Protect Password:="npssadmin"
End Sub

I want code to paste the images in as "ImgG" or "ImgJ", not image1 or image2. I will then update the delete code.


What code do I use?
 
Upvote 0
The code you posted uses an OR statement, whereas mine used an AND statement.

Code:
If Pic.Name = "Image1" Or Pic.Name = "Image2" Then

Make sure you use the right one, there is a big difference !!

I want code to paste the images in as "ImgG" or "ImgJ", not image1 or image2
So why not simply rename them
Code:
If Pic.Name = "Image1" then pic.name = "ImgG"
 
Upvote 0
Is there code to delete only images on the sheet as my command buttons are being deleted now?
 
Upvote 0
Yes, using the naming convention for them as well
Code:
 If Pic.Name <> "CmdButton1" And Pic.Name <> "CmdButton2" Then
 
Upvote 0
Thanks Michael, I got it working with this code.

Code:
Private Sub cmdNoSig_Click()
ActiveSheet.Unprotect Password:="npssadmin"
Dim Pic As Object
For Each Pic In ActiveSheet.Pictures
    If Pic.Name <> "lblActivities" And Pic.Name <> "lblNotes" And Pic.Name <> "cmdAddRow" And Pic.Name <> "cmdDeleteRow" And _
    Pic.Name <> "cmdDelSelect" And Pic.Name <> "cmdG" And Pic.Name <> "cmdJ" And Pic.Name <> "cmdNoSig" And Pic.Name <> "cmdSend" And _
    Pic.Name <> "cmdDeleteQuoteLines" And Pic.Name <> "imgLogo" Then
         ' If Not Intersect(Pic.TopLeftCell, Range("A12:A300")) Is Nothing Then
              Pic.Delete
          'End If
    End If
Next Pic
'ActiveSheet.Protect Password:="npssadmin"
End Sub

What does this line of code do that I had commented out?
Code:
' If Not Intersect(Pic.TopLeftCell, Range("A12:A300")) Is Nothing Then
 
Upvote 0
If you hadn't commented it out....any pics with their TopLeft outside the range wouldn't be affected !!
 
Last edited:
Upvote 0
Code:
' If Not Intersect(Pic.TopLeftCell, Range("A12:A300")) Is Nothing Then

That commented line of code does not make sense. Is it a double negative? Could you describe it to me in words please?
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,595
Members
452,656
Latest member
earth

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