Dynamic Picture Change in Range

Senthil Murugan

Board Regular
Joined
Sep 25, 2024
Messages
54
Office Version
  1. 365
Platform
  1. Windows
Good Evening Everybody

Can anybody else help me to get the code for following my work

I have some material images in Folder ( D:\Images )
In sheet 1, Column A , i have list of material name belongs to that folder

what i need is
if i put any name of material in column A, in corresponding row in column A, the image shall come

Further

if i delete or clear any image, the corresponding image also to be deleted

Further

if i change name in existing, the corresponding image shall also be deleted and new image shall come according to new name change


with regards


A.Senthil Murugan
 
if i put any name of material in column A, in corresponding row in column B the image shall come

VBA Code:
Sub DeletePictureBasedOnCellValue()
   Dim shp As Shape
    Dim targetCell As Range
    Dim offsetCell As Range
    Dim valueCondition As String
    
    ' Set target cell and the offset cell (adjust offset as needed)
    Set targetCell = ActiveSheet.Range("A1") ' The cell you are checking (target)
    Set offsetCell = targetCell.Offset(0, 1) ' The adjacent cell (to the right)
    
    ' Define the condition (e.g., delete picture if the offset cell is "Delete")
    valueCondition = "Delete"
    
    ' Loop through all shapes (including pictures) in the sheet
    For Each shp In ActiveSheet.Shapes
        ' Check if the shape is a picture and corresponds to the target cell
        If shp.Type = msoPicture Then
            ' If the picture's top-left corner intersects the target cell
            If Not Intersect(shp.TopLeftCell, targetCell) Is Nothing Then
                ' If the offset cell value matches the condition, delete the picture
                If offsetCell.Value = valueCondition Then
                    shp.Delete
                End If
            End If
        End If
    Next shp
 
Upvote 0
I am using the following code
VBA Code:
Dim lastRow As Long

Sub InsertImages111()
    Dim imgFolder As String
    Dim cell As Range
    Dim imgPath As String
    Dim img As Picture
    Dim imgName As String

    ' Folder where images are stored
    imgFolder = "D:\Images\" ' Update with the correct folder path
    
    ' Get the last row of data in Column A
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    ' Loop through each cell in Column A
    For Each cell In Range("A1:A" & lastRow)
        imgPath = imgFolder & cell.Value & ".jpg" ' Assumes image files are named exactly as the values in Column A with .jpg extension
        imgName = "Image_" & cell.Row ' Name of the image to be used for identification

        ' Check if the image exists
        If Len(Dir(imgPath)) > 0 Then
            ' Delete any existing image before inserting a new one
            On Error Resume Next
            Set img = cell.Offset(0, 1).Parent.Shapes(imgName)
            On Error GoTo 0
            If Not img Is Nothing Then
                img.Delete ' Delete the old image if it exists
            End If

            ' Insert the new image
            Set img = cell.Offset(0, 1).Parent.Pictures.Insert(imgPath)
            With img
                .ShapeRange.LockAspectRatio = msoFalse
                .Top = cell.Offset(0, 1).Top
                .Left = cell.Offset(0, 1).Left
                .Width = 50 ' Adjust size as needed
                .Height = 50 ' Adjust size as needed
                .Name = imgName ' Name the image for easy reference
            End With
        Else
            ' If image doesn't exist, delete any existing image in the corresponding cell
            On Error Resume Next
            Set img = cell.Offset(0, 1).Parent.Shapes(imgName)
            On Error GoTo 0
            If Not img Is Nothing Then
                img.Delete ' Delete the old image if it exists
            End If
        End If
    Next cell
End Sub
 
Upvote 0
Hi Senthil Murugan. You are inserting pictures. Therefore, to delete the pic you need to use pic type linked picture. Not sure if you need any more help? Dave
VBA Code:
If shp.type = msoLinkedPicture then
 
Upvote 0

Forum statistics

Threads
1,226,785
Messages
6,192,967
Members
453,770
Latest member
mwedom

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