Troubleshooting Loop In PictureInsert Code

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
781
Office Version
  1. 2016
Platform
  1. 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")


O0aT6PA.png
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
At a guess the when an image doesn't exist the previous image is still selected, try
Code:
    If Pshp Is Nothing Then Range("H" & i).Value = 0
    Set Pshp = Nothing
    [COLOR=#ff0000]Range("a1").Select[/COLOR]
 
Upvote 0
At a guess the when an image doesn't exist the previous image is still selected, try
Code:
    If Pshp Is Nothing Then Range("H" & i).Value = 0
    Set Pshp = Nothing
    [COLOR=#ff0000]Range("a1").Select[/COLOR]

huh.... thats a strange interaction but i'll take it, thank you.
that solved both issues
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
How about
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
      With ActiveSheet.Pictures.Insert(filenam)
             If Err = 1004 Then Range("H" & i) = 0 Else Range("H" & i) = 1
            .LockAspectRatio = msoFalse
            .Width = 15
            .Height = 15
            .Top = trgtRng.Top
            .Left = trgtRng.Left
    End With
    On Error GoTo 0
lastline:
    Next i
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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