I have two columns:
A B
1 Animal Picture
2 Lion (Lion picture)
3 Ant (Ant picture)
When I type an animal name in a new cell (lets say A4
), the formula works perfectly: I get the picture in the picture column (B
).
If I delete a value in cloumn A
(lets say I delete Lion) then the picture of Lion gets deleted.
But when I edit manually without deleting value in A2
, a new picture overlaps B2
above the last one. When I delete that A2
value, only the latest picture get deleted. I have to delete again empty cell A2
to delete remaining picture in cell B2
.
Is there any way to fix this issue?
Here is my current Worksheet_Change
event code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo son
If Intersect(Target, [A:A]) Is Nothing Then Exit Sub
If Target.Row Mod 20 = 0 Then Exit Sub
If Not IsEmpty(Target) Then '<--| if changed cell content is not empty
With Pictures.Insert(ThisWorkbook.Path & "\" & Target.Value & ".png")
.Top = Target.Offset(0, 2).Top
.Left = Target.Offset(0, 1).Left
.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = Target.Offset(0, 2).Height
.ShapeRange.Width = Target.Offset(0, 2).Width
.Name = Target.Address '<--| associate the picture to the edited cell via its address
End With
Else '<--| if cell content has been deleted
Me.Shapes(Target.Address).Delete '<--| delete the picture whose name is associated to the cell via its address
End If
Target.Offset(1, 0).Select
son:
End Sub