VBA to Delete a picture before adding one to target cell

DCGNM

New Member
Joined
Feb 25, 2019
Messages
18
Hi All

I have had some help with another user on how to create a button whereby I could open a popup, select a picture, have it stretched to fix and centered in a cell.

However, i just noticed that if there was an existing picture there, it would not be deleted, and the new picture that i selected would just be added on top of the original picture. Can i ask for help to add in one more code to clear/delete the cell of any pictures first before adding the new picture?

---------------------------------------------------------------------------
Private Sub CommandButton6_Click()
Dim Pic As Object
Dim rng As Range: Set rng = Sheets("Cover Page").Range("C7").MergeArea

With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Select an image file"
.Filters.Clear
.Filters.Add "JPG", "*.JPG"
.Filters.Add "Graphics Interchange Format", "*.GIF"
.Filters.Add "Portable Network Graphics", "*.PNG"
.Filters.Add "All Pictures", "*.*"


If .Show = -1 Then


Set Pic = Sheets("Cover Page").Pictures.Insert(.SelectedItems(1))
With rng
Pic.Top = .Top
Pic.Height = .Height
If Pic.Width > .Width Then Pic.Width = .Width
Pic.Left = .Left + 0.5 * (.Width - Pic.Width)
End With
End If
End With
End Sub
 
Last edited:

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.
.
See if this works :

Code:
Private Sub CommandButton6_Click()
Dim Pic As Object
Dim rng As Range: Set rng = Sheets("Cover Page").Range("C7").MergeArea


Sheets("Cover Page").Range("C7").ClearContents


With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Select an image file"
 
Upvote 0
With the following, the image is named as "picture", then the next time the image called "picture" is deleted, each time you execute the macro, it will erase the image "picture" and the new one will be called "picture"

Code:
Private Sub CommandButton6_Click()
Dim Pic As Object
Dim rng As Range: Set rng = Sheets("Cover Page").Range("C7").MergeArea


With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    .Title = "Select an image file"
    .Filters.Clear
    .Filters.Add "JPG", "*.JPG"
    .Filters.Add "Graphics Interchange Format", "*.GIF"
    .Filters.Add "Portable Network Graphics", "*.PNG"
    .Filters.Add "All Pictures", "*.*"
    
    
    If .Show = -1 Then
    
    
        Set Pic = Sheets("Cover Page").Pictures.Insert(.SelectedItems(1))
        With rng
[COLOR=#0000ff]            On Error Resume Next[/COLOR]
[COLOR=#0000ff]            Sheets("Cover Page").Pictures("picture").Delete[/COLOR]
[COLOR=#0000ff]            On Error GoTo 0[/COLOR]
[COLOR=#0000ff]            Pic.Name = "picture"[/COLOR]
            
            Pic.Top = .Top
            Pic.Height = .Height
            If Pic.Width > .Width Then Pic.Width = .Width
            Pic.Left = .Left + 0.5 * (.Width - Pic.Width)
        End With
    End If
End With
End Sub
 
Upvote 0
.
Merge cells are nice for visual display of information but can be a frustration to no end.

Always best to avoid merged cells.
 
Upvote 0
I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
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