ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Deleting images in Excel document (https://www.excelbanter.com/excel-programming/386093-deleting-images-excel-document.html)

[email protected]

Deleting images in Excel document
 
Here's my dilemma: I have a worksheet which on the click of 'Command
Button1' unprotects the worksheet, inserts a desired image into a
group of merged cells identified as cell L43 and then reprotects the
worksheet. I then have a second command button (Command Button2) that
when clicked unprotects the worksheet, removes the image from this
same group of cells and then reprotects the worksheet. Unfortunately,
when I click this Command2 button, it not only removes the image in
cell L43, it also removes every other image in the worksheet. I only
want the images in cell L43 to be removed. Any help would be
appreciated! Here's the Command2 button code:


Private Sub CommandButton2_Click()

Application.ActiveSheet.Unprotect "password"

For Each Picture In Pictures

'ActiveSheet.Pictures.Delete
'Debug.Print Picture.Name

If Left(Picture.Name, 7) = "Picture" Then

Picture.Delete

End If

Next

Application.ActiveSheet.Protect "password"

End Sub


JE McGimpsey

Deleting images in Excel document
 
One way:

Private Sub CommandButton2_Click()
Dim oPic As Picture
With Application.ActiveSheet
.Unprotect "password"
For Each oPic In .Pictures
If oPic.TopLeftCell.Address(False, False) = "L43" Then _
oPic.Delete
Next oPic
.Protect "password"
End With
End Sub


In article .com,
wrote:

Here's my dilemma: I have a worksheet which on the click of 'Command
Button1' unprotects the worksheet, inserts a desired image into a
group of merged cells identified as cell L43 and then reprotects the
worksheet. I then have a second command button (Command Button2) that
when clicked unprotects the worksheet, removes the image from this
same group of cells and then reprotects the worksheet. Unfortunately,
when I click this Command2 button, it not only removes the image in
cell L43, it also removes every other image in the worksheet. I only
want the images in cell L43 to be removed. Any help would be
appreciated! Here's the Command2 button code:


Private Sub CommandButton2_Click()

Application.ActiveSheet.Unprotect "password"

For Each Picture In Pictures

'ActiveSheet.Pictures.Delete
'Debug.Print Picture.Name

If Left(Picture.Name, 7) = "Picture" Then

Picture.Delete

End If

Next

Application.ActiveSheet.Protect "password"

End Sub


NickHK

Deleting images in Excel document
 
Pictures are not "in cell", so the easiest way is to use the shapes name.
You can set this when you insert the picture.
If you have other control on the WS that you wish to keep, check what the
shape is before deleting.

Private Sub CommandButton1_Click()
ActiveSheet.Shapes("PicToDelete").Delete
End Sub

NickHK

wrote in message
oups.com...
Here's my dilemma: I have a worksheet which on the click of 'Command
Button1' unprotects the worksheet, inserts a desired image into a
group of merged cells identified as cell L43 and then reprotects the
worksheet. I then have a second command button (Command Button2) that
when clicked unprotects the worksheet, removes the image from this
same group of cells and then reprotects the worksheet. Unfortunately,
when I click this Command2 button, it not only removes the image in
cell L43, it also removes every other image in the worksheet. I only
want the images in cell L43 to be removed. Any help would be
appreciated! Here's the Command2 button code:


Private Sub CommandButton2_Click()

Application.ActiveSheet.Unprotect "password"

For Each Picture In Pictures

'ActiveSheet.Pictures.Delete
'Debug.Print Picture.Name

If Left(Picture.Name, 7) = "Picture" Then

Picture.Delete

End If

Next

Application.ActiveSheet.Protect "password"

End Sub





All times are GMT +1. The time now is 11:26 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com