DataBlake
Well-known Member
- Joined
- Jan 26, 2015
- Messages
- 781
- Office Version
- 2016
- Platform
- Windows
Code:
Sub URLPictureInsert()
Dim Pshp As Shape
Dim i As Long
Dim lastRow As Long
Dim urlRng As Range
Dim trgtRng As Range
lastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow
Set urlRng = Range("B" & i)
Set trgtRng = Range("C" & i)
If urlRng = "" Then Range("H" & i).Value = 0
If urlRng = "" Then GoTo lastline
filenam = urlRng
On Error Resume Next
ActiveSheet.Pictures.Insert(filenam).Select
On Error Resume Next
Set Pshp = Selection.ShapeRange.Item(1)
On Error GoTo 0
If Pshp Is Nothing Then GoTo lab
Range("H" & i).Value = 1
With Pshp
.LockAspectRatio = msoFalse
.Width = 15
.Height = 15
.Top = trgtRng.Top
.Left = trgtRng.Left
End With
lab:
If Pshp Is Nothing Then Range("H" & i).Value = 0
Set Pshp = Nothing
lastline:
Next i
End Sub
So i have this code that inserts images into column C but there is a problem in that it keeps putting the last correct image at the last incorrect image. It creates this +1 offset by doing this and i'm unsure of where in my code this is happening. I will attach a picture to illustrate.
As well is there a surefire way to put "0" in column H if the image does not work? meaning the url does in fact go to a webpage, but the image does not exist there. Image not found still equates to a "1" in column H
example images used:
https://cookieandkate.com/images/2018/04/delicious-vegetarian-tacos-recipe.jpg (correct image)
https://cookieandkate.com/images/2018/04/delicious-vegetarian-tacos-ree (incorrect image that still produces a "1")
https://i733.photobucket.com/albums/ww338/ShopLAOnline/back_heyhole_top_ella_red.jpg (incorrect image that still produces a "1")