VBA Code - Show a picture and delete it if there is none.

Tonkytonk

New Member
Joined
Apr 9, 2020
Messages
7
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

I've been playing around with a lot of code lately and this is the best that I have been able to do so far. I would really love hover over function instead but I'm not that good.

My problem is I can get the image inserted in to K1 but I have an activeX control in A1 and that gets deleted if I use this ActiveSheet.Pictures.Delete it deletes everything.

I can't remember the other thing I do but then it doesn't delete the picture in K1 just puts a new image over the top.

So finally I thought to do an If Statement but have completely buggered it up. Hoping someone could help me out.

Code:
If ActiveSheet.Shapes.Range(Array("Picture *")).Count > 0 Then
        ActiveSheet.Shapes.Range(Array("Picture *")).Delete
    End If


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim mypic As Picture
    Dim PicLoc As String
  
    If Target.Address = "$B$" & ActiveCell.Row Then
      
        'ActiveSheet.Pictures.Delete
    If ActiveSheet.Shapes.Range(Array("Picture *")).Count > 0 Then
        ActiveSheet.Shapes.Range(Array("Picture *")).Delete
    End If
      
        PicLoc = "O:\2023\School Photos\2023\Students\" & ActiveCell.Value & ".jpg"
      
        On Error Resume Next
        Set mypic = ActiveSheet.Pictures.Insert(PicLoc)
        On Error GoTo 0
      
        If mypic Is Nothing Then
            Range("K1").Value = "No Picture"
        Else
            With Range("K1")
                .Value = ""
                .RowHeight = mypic.Height
                mypic.Top = .Top
                mypic.Left = .Left
                mypic.Placement = xlMoveAndSize
            End With
        End If
      
    End If

End Sub
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi all,

I've been playing around with a lot of code lately and this is the best that I have been able to do so far. I would really love hover over function instead but I'm not that good.

My problem is I can get the image inserted in to K1 but I have an activeX control in A1 and that gets deleted if I use this ActiveSheet.Pictures.Delete it deletes everything.

I can't remember the other thing I do but then it doesn't delete the picture in K1 just puts a new image over the top.

So finally I thought to do an If Statement but have completely buggered it up. Hoping someone could help me out.

Code:
If ActiveSheet.Shapes.Range(Array("Picture *")).Count > 0 Then
        ActiveSheet.Shapes.Range(Array("Picture *")).Delete
    End If


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim mypic As Picture
    Dim PicLoc As String
 
    If Target.Address = "$B$" & ActiveCell.Row Then
     
        'ActiveSheet.Pictures.Delete
    If ActiveSheet.Shapes.Range(Array("Picture *")).Count > 0 Then
        ActiveSheet.Shapes.Range(Array("Picture *")).Delete
    End If
     
        PicLoc = "O:\2023\School Photos\2023\Students\" & ActiveCell.Value & ".jpg"
     
        On Error Resume Next
        Set mypic = ActiveSheet.Pictures.Insert(PicLoc)
        On Error GoTo 0
     
        If mypic Is Nothing Then
            Range("K1").Value = "No Picture"
        Else
            With Range("K1")
                .Value = ""
                .RowHeight = mypic.Height
                mypic.Top = .Top
                mypic.Left = .Left
                mypic.Placement = xlMoveAndSize
            End With
        End If
     
    End If

End Sub
Hi all,

Just wanted to give an update incase anyone was going to help me out and if you want to give any advice i still would appreciate it

What I have done is now define the picture as a name
mypic.Name = "Picture"

and then delete it
On Error Resume Next ActiveSheet.Shapes("Picture").Delete On Error GoTo 0
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,633
Members
452,661
Latest member
Nonhle

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