I'm rather green with programming, and I found some code on stackoverflow. Original source: Insert Dynamically in Excel using image from server path It works, but if the loop gets to an empty cell, it crashes. (Run-time error '1004') I believe that skipping the empty cells should avoid the issue. I'm thinking it's something simple, but I can't find it online. I tried using GoTo to skip past the error but it's seems to skip everything. I apologize if I'm not including enough information.
Sub ImageUpdateF()
' inserts the picture files listed in column G into the
' column G cells in the workbook.
Dim cell As Range
Dim sFile As String
Dim shpPic As Shape
Dim ws As Worksheet: Set ws = ActiveSheet
With ws
For Each cell In .Range(.Range("G2"), .Cells(.Rows.Count, "G").End(xlUp))
sFile = cell.Text
If ActiveCell.Value = vbNullString Then
GoTo NextIteration
ElseIf Len(Dir(sFile)) Then
Set shpPic = .Shapes.AddPicture2(sFile, msoFalse, msoTrue, 0, 0, -1, -1, 1)
shpPic.LockAspectRatio = msoTrue
With cell.Offset(, 0)
If shpPic.Height > .Height Then shpPic.Height = .Height
If shpPic.Width > .Width Then shpPic.Width = .Width
shpPic.Top = .Top + .Height / 2 - shpPic.Height / 2
shpPic.Left = .Left + .Width / 2 - shpPic.Width / 2
End With
End If
'label
NextIteration:
Next cell
End With
End Sub
Array setup
| Column F | Column G | Column H
| -------- | --------- | --------
| file1 loc| file1.2 loc| file1.3 loc
| file2 loc| Empty | Empty
| file3 loc| file3.2 loc| Empty
I also tried putting an Next cell statement after the initial If statement, but I got an error. Like I said, I'm pretty green.