deleting images

  • Thread starter Thread starter Kevin Carter
  • Start date Start date
K

Kevin Carter

hi
i have a workbook with two worksheets
worksheet 1 (main) is the input screen
worksheet 2 (images) contains a number of images(giffs)
each image is assigned a macro that once clicked the image is copied and
pasted onto the main worksheet at a given cell location

ActiveSheet.Shapes("Picture 4").Select
Selection.Copy
ThisWorkbook.Worksheets("MAIN").Select
Range("d9").Select
ActiveSheet.PasteSpecial Format:="Picture (GIF)", Link:=False, _
DisplayAsIcon:=False
Application.CommandBars("Picture").Visible = False

the picture number is different for each picture

what i want to do is when a second image is selected to be pasted onto main
worksheet delete the image that is already there
is this possible?


thanks


kevin
 
Do all the pictures go into the same pasted location?

Are there any pictures on the Main worksheet that need to be kept?
If you can delete all of them, you could use a line like this in your code:

activesheet.pictures.delete

If you have to delete one, but keep others, you could loop through the
..topleftcell of the pictures and see if it's in a range to be deleted.

Another way is to name the pictures nicely when you add them. Then you can use
the name of the picture to delete.

=====
If each of the pictures replaces the existing one, you could use code like this
(and this single macro is assigned to each of the pictures):

Option Explicit
Sub pictClick()

Dim myPict As Picture
Set myPict = ActiveSheet.Pictures(Application.Caller)

myPict.Copy
With ThisWorkbook.Worksheets("MAIN")
On Error Resume Next
.Pictures("MyPicture").Delete
On Error GoTo 0
.Select
.Range("d9").Select
.Paste
.Pictures(.Pictures.Count).Name = "MyPicture"
End With

End Sub

the application.caller stuff returns the name of the picture that you just
clicked.

It deletes the picture on Main called "MyPicture", then adds one and renames it
to "MyPicture".

======
It doesn't sound like this fits your situation, but if you have lots of pictures
to keep and one to delete, you could use a name that includes the address of the
that topleftcell. Then delete it when you want.


Option Explicit
Sub pictClick2()

Dim myPict As Picture
Dim myAddr As String

Set myPict = ActiveSheet.Pictures(Application.Caller)

Select Case LCase(myPict.Name)
Case Is = "picture 1": myAddr = "D9"
Case Is = "picture 2": myAddr = "e13"
Case Is = "picture 3": myAddr = "f1"
Case Else
MsgBox "design error--contact ????"
Exit Sub
End Select

myPict.Copy
With ThisWorkbook.Worksheets("MAIN")
On Error Resume Next
.Pictures("MyPicture_" & myAddr).Delete
On Error GoTo 0
.Select
.Range(myAddr).Select
.Paste
.Pictures(.Pictures.Count).Name = "MyPicture_" & myAddr
End With

End Sub
 
Back
Top