VBA自动载入照片的问题

taeshali 2013-10-18 08:47:17
Sub ZAIRU()
On Error Resume Next
Dim R&
Dim Pic As Object
For Each Pic In Sheet1.Shapes
If Pic.Name <> Sheet1.Shapes("按钮 97").Name Then
Pic.Delete
End If
Next
For R = 3 To Range("B65536").End(xlUp).Row
If (Len(Cells(R, 2)) <> 0) Then
Set Pic = Sheet1.Pictures.Insert(ThisWorkbook.Path & "\图片\" & Cells(R, 2) & ".jpg")
Pic.ShapeRange.LockAspectRatio = True
With Pic.ShapeRange
If .Height / .Width < Cells(R, 1).Height / Cells(R, 1).Width Then
.Width = Cells(R, 1).Width
.Left = Cells(R, 1).Left
.Top = Cells(R, 1).Top + (Cells(R, 1).Height - .Height) / 2
Else
.Height = Cells(R, 1).Height
.Top = Cells(R, 1).Top
.Left = Cells(R, 1).Left + (Cells(R, 1).Width - .Width) / 2
End If
End With
End If
Next R
End Sub

这个自动载入图片的宏,载入图片是可以的,但是如果如果哪一行上对应的图片没有的话,则上边一行的图片就会挪到下边来,比如说,A2中的数据是A,则第一遍for的时候"A.jpg"就载入A1里.然后执行第二遍for,B2里的数据是B,但是如果刚好没有"B.jpg"这张图片,这时候B1里应该是空的,继续执行下次的for,但是实际是A.jpg会落到B1里,A1里成空的了,请问这个问题怎么解决啊?
...全文
453 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
可乐罐 2014-02-18
  • 打赏
  • 举报
回复
应该有一个空的判断,如果是空,则自动跳过这个单元格(cell) 我想应该是: Set Pic = Sheet1.Pictures.Insert(ThisWorkbook.Path & "\图片\" & Cells(R, 2) & ".jpg") 后判断下Pic对象是否为空,为空则不执行后面的语句,回到下一个for循环
暱稱 2014-01-26
  • 打赏
  • 举报
回复
引用 1 楼 dsd999 的回复:
Set Pic = Nothing
Set Pic = Sheet1.Pictures.Insert(ThisWorkbook.Path & "\My Pictures\" & Cells(R, 2) & ".jpg")
请问版主 Set Pic = Nothing 这条要放哪里,用半天图片还是会跑掉,可以的话希望有完整代码,谢谢
taeshali 2013-10-30
  • 打赏
  • 举报
回复
谢谢版主!郁闷了好几天啊。谢谢啦!
dsd999 2013-10-21
  • 打赏
  • 举报
回复
Set Pic = Nothing
Set Pic = Sheet1.Pictures.Insert(ThisWorkbook.Path & "\My Pictures\" & Cells(R, 2) & ".jpg")

5,139

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 Office开发/ VBA
社区管理员
  • Office开发/ VBA社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧