[英]Show pic in column next to non empty cell in column
I'd like to show a pic loaded from network location next to each non empty cell in range A1:A10 and name the picture after the value in that range.我想在 A1:A10 范围内的每个非空单元格旁边显示从网络位置加载的图片,并在该范围内的值之后命名图片。
Sub testpics()
Dim Cell As Range
For Each Cell In Range("A1:A10").Cells
If Not IsEmpty(Cell) Then
On Error GoTo ErrNoPhoto
pictureloc = "location here" & Cell & ".jpg"
With Cell.Offset(0, 1)
Set mypict = ActiveSheet.Pictures.Insert(pictureloc)
mypict.Height = .RowHeight
mypict.Left = .Left
mypict.Top = .Top
mypict.Placement = xlMoveAndSize
mypict.Name = Cell
mypict.OnAction = "enlarge"
End With
Exit Sub
ErrNoPhoto:
MsgBox "Unable to Find Photo for " & Cell
End If
Next Cell
End Sub
So far I came up with the code above, but it only shows the pic behind A1 if I run it.到目前为止,我想出了上面的代码,但如果我运行它,它只会显示 A1 后面的图片。 What am I doing wrong/missing in my code?我在代码中做错了什么/遗漏了什么?
The main issue is that you exit the loop after the first iteration.主要问题是您在第一次迭代后退出循环。 However, I refactored your code below to make it more stable, readable and better actual error trapping.但是,我重构了下面的代码,使其更加稳定、可读和更好的实际错误捕获。
Option Explicit
Sub testpics()
Dim Cell As Range
For Each Cell In Range("A1:A10").Cells
If Not IsEmpty(Cell) Then
Dim pictureLoc As String
pictureLoc = "location here" & Cell & ".jpg"
Dim mypict As Object
On Error Resume Next
Set mypict = ActiveSheet.Pictures.Insert(pictureLoc)
On Error GoTo 0
If Not mypict Is Nothing Then
With Cell.Offset(0, 1)
mypict.Height = .RowHeight
mypict.Left = .Left
mypict.Top = .Top
mypict.Placement = xlMoveAndSize
mypict.Name = Cell.Value
mypict.OnAction = "enlarge"
End With
Else
Cell.Offset(0, 1).Value = "Could Not Find Pic"
End If
End If
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.