I want to insert pictures in an Excel sheet next to its model name.
There is a loop to insert pictures row by row till last model.
The problem occurs when ppath is empty (blank http page). If empty the macro should go to Next
.
On error resume next
is crashing Excel. Error handler I tried is not working also (getting debug code on ppath).
How do I skip to Next
when ppath is empty (no picture)?
Here is my code:
Sub insert_foto()
Dim i As Long
Dim ppath As String
Dim lastrow As Long
Dim ws As Worksheet
Application.ScreenUpdating = False
Set ws = ThisWorkbook.Worksheets("sheet")
lastrow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
With ws
ws.Range("A6:A" & lastrow).RowHeight = 90
End With
'On Error Resume Next - is crashing excel when ppath is empty
On Error GoTo ErrHandler
For i = 6 To lastrow
' picture name in col A, ppath is where the pictures are
ppath = "http://aa/bb/" & CStr(Cells(i, 2).Value & "-F1.jpg")
'If ppath <> "" Then -tried this solution but not working
With ActiveSheet.Shapes.AddPicture(Filename:=ppath, linktofile:=msoFalse, _
savewithdocument:=msoCTrue, Left:=0, Top:=0, Width:=50, Height:=85)
.Left = ActiveSheet.Cells(i, 1).Left + (ActiveSheet.Cells(i, 1).Width - .Width) / 2
.Top = ActiveSheet.Cells(i, 1).Top + (ActiveSheet.Cells(i, 1).Height - .Height) / 2
.Placement = 1
End With
' End If
ErrHandler:
'Resume Next
Next
Application.ScreenUpdating = True
End Sub
If Not IsEmpty(Cells(i, 2).Value) Then
. - BigBen