VBA Remove image link

sanket_sk

Board Regular
Joined
Dec 27, 2016
Messages
140
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am stuck in one of unique issue.

I was looking for a macro that will help to insert an image based on cell value.

I got the macro on the internet and modified it as per my requirement, this macro is working exactly the way I want however when I change or delete the image folder all the images are going vanished.

I tried to remove a link from an image but not succeeded, could you please help me understand what went wrong and how I can remove the link from the folder.

You may suggest another code also which will solve my problem.


Regards,
Sanket

VBA Code:
Sub InsertPictures()


    Const fPath = "C:\Desktop\Final snap as 0n 28 Jun\"

    Dim cel As Range, picPath As String, shp As Shape
'insert imagess
    For Each cel In Range("M4", Range("M" & Rows.Count).End(xlUp))
        On Error Resume Next
        picPath = fPath & "\" & cel.Value & ".jpg"
        If Not Dir(picPath, vbDirectory) = vbNullString Then
            With cel.Parent.Pictures.Insert(picPath, LinkToFile = False)
                With .ShapeRange
                    .LinkToFile = msoFalse
                    .savewithdocument = msoCTrue
                    .LockAspectRatio = msoFalse
                    .LinkToFile = False
                    .savewithdocument = True
                    .LockAspectRatio = False
                    .Width = 100
                    .Height = 100
                End With
                .Left = cel.Offset(, 1).Left
                .Top = cel.Offset(, 1).Top
            End With
        End If
    Next cel
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try the below:
VBA Code:
Sub InsertPictures()
    Dim cel As Range, picPath As String, shp As Shape
    
    Const fPath = "C:\Users\jbloggs\Desktop\test"
    
    For Each cel In Range("A1", Range("A" & Rows.Count).End(xlUp))
        picPath = fPath & "\" & cel.Value & ".jpg"
        If Not Dir(picPath, vbDirectory) = vbNullString Then
            Set shp = Sheet1.Shapes.AddPicture(picPath, msoFalse, msoCTrue, 0, 0, 100, 100)
            With shp
                .Left = cel.Offset(, 1).Left
                .Top = cel.Offset(, 1).Top
            End With
        End If
    Next cel
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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