【发布时间】:2015-03-24 09:54:16
【问题描述】:
我创建了一个代码,用于将图片从链接(在该单元格旁边)插入到单元格中。有时,图片链接到的文件中的图片会被删除。我收到一个错误 400,但是当我输入“on error resume next”时,它会将最后一个带有正确链接的单元格留空,并将该图片放入带有错误链接的单元格中。最后一个带有正确链接的单元格也是空的。
'on error resume next' 的位置无关紧要(在循环之前,或在循环中的任何位置)
我怎样才能避免这种情况?跳过错误的链接,把图片放在正确的位置?
Sub InsertPictures()
Call DeleteAllPicturesInRange
Dim pic As String
Dim myPicture As Picture
Dim rng As Range
Dim cl As Range
Set rng = Range("J5:J124")
For Each cl In rng
pic = cl.Offset(0, 1)
Set myPicture = ActiveSheet.Pictures.Insert(pic)
With myPicture
.ShapeRange.LockAspectRatio = msoFalse
.Width = cl.Width
.Height = cl.Height
.Top = Rows(cl.Row).Top
.Left = Columns(cl.Column).Left
End With
Next
End Sub
【问题讨论】:
-
这里的教训:除非你知道错误是什么,否则不要使用
On Error Resume Next!