Excel精英培训网

 找回密码
 注册
查看: 2740|回复: 10

如何遍历图片?

[复制链接]
发表于 2013-4-12 19:40 | 显示全部楼层 |阅读模式
本帖最后由 不信这样还重名 于 2013-4-12 21:13 编辑

求段代码,要求达到功能:如果A2,C2中有图片,则Exit sub,若哪个没有,则在对应的单元格中写上“A2无图片”/“C2无图片",别的单元格中的图别动。
注:表中还有别的图片,这个图片的总数怎么得到呢?如果没有图片总数如何可以遍历所有图片?{:281:}
表格大概样子如下 遍历图片.zip (13.16 KB, 下载次数: 1)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-4-12 20:42 | 显示全部楼层
回复

使用道具 举报

发表于 2013-4-12 20:52 | 显示全部楼层
  1. Sub test()
  2.     Dim shap As Object
  3.     For Each shap In ActiveSheet.Shapes
  4.         Debug.Print shap.Name, shap.TopLeftCell.Address(False, False)
  5.     Next
  6. End Sub
复制代码
看立即窗口的输出。
回复

使用道具 举报

发表于 2013-4-12 21:01 | 显示全部楼层
本帖最后由 hwc2ycy 于 2013-4-12 21:02 编辑
  1. Sub 宏1()
  2.     Dim shap As Object
  3.     Dim pica2 As Boolean
  4.     Dim picc2 As Boolean
  5.     For Each shap In ActiveSheet.Shapes
  6.         Select Case shap.TopLeftCell.Address(False, False)
  7.             Case "A2": pica2 = True
  8.             Case "C2": picc2 = True
  9.         End Select
  10.         If pica2 And pica2 Then Exit For
  11.     Next
  12.     If Not pica2 Then [a2] = "A2无图片"
  13.     If Not picc2 Then [c2] = "C2无图片"
  14. End Sub
复制代码
回复

使用道具 举报

发表于 2013-4-12 21:02 | 显示全部楼层
要写入的单元格的话,可以用字典来实现。
回复

使用道具 举报

 楼主| 发表于 2013-4-12 21:13 | 显示全部楼层
做了一个附件发了上来

遍历图片.zip

13.16 KB, 下载次数: 0

回复

使用道具 举报

发表于 2013-4-12 21:23 | 显示全部楼层
  1. Sub 宏1()


  2.     If Not isCellHasPic("c2") Then [C2] = "C2无图片"
  3.     If Not isCellHasPic("a2") Then [a2] = "C2无图片"

  4. End Sub


  5. Function isCellHasPic(str As String)
  6.     On Error Resume Next
  7.     If Range(str) Is Nothing Then Exit Function
  8.    
  9.     If Err.Number <> 0 Then
  10.         MsgBox "参数无效,非有效的单元格地址"
  11.         Exit Function
  12.     End If
  13.    
  14.     Dim shap As Shape

  15.     Dim dic As Object
  16.     Set dic = CreateObject("scripting.dictionary")

  17.     For Each shap In ActiveSheet.Shapes
  18.         dic(shap.TopLeftCell.Address(False, False)) = ""
  19.     Next
  20.    
  21.     If dic.exists(str) Then isCellHasPic = True
  22.    
  23. End Function
复制代码
回复

使用道具 举报

发表于 2013-4-12 21:25 | 显示全部楼层
  1. Sub 宏1()


  2.     If Not isCellHasPic("A2") Then
  3.         [c2] = "C2无图片"
  4.     Else
  5.         [c2] = ""
  6.     End If
  7.         
  8.     If Not isCellHasPic("A2") Then
  9.         [a2] = "A2无图片"
  10.     Else
  11.         [a2] = ""
  12.     End If

  13. End Sub


  14. Function isCellHasPic(str As String)
  15.     On Error Resume Next
  16.     If Range(str) Is Nothing Then Exit Function
  17.    
  18.     If Err.Number <> 0 Then
  19.         MsgBox "参数无效,非有效的单元格地址"
  20.         Exit Function
  21.     End If
  22.    
  23.     Dim shap As Shape
  24.     Dim dic As Object
  25.    
  26.     Set dic = CreateObject("scripting.dictionary")

  27.     For Each shap In ActiveSheet.Shapes
  28.         dic(shap.TopLeftCell.Address(False, False)) = ""
  29.     Next
  30.    
  31.     If dic.exists(UCase(str)) Then isCellHasPic = True
  32.    
  33.     Set dic = Nothing
  34. End Function
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-4-12 21:47 | 显示全部楼层
hwc2ycy 发表于 2013-4-12 21:01

呃,终于看懂了,班亲的代码很节省字符{:091:}莫名了半天,总算是懂了,十分感谢{:021:}
回复

使用道具 举报

发表于 2013-4-12 21:51 | 显示全部楼层
用最后贴的方法,已经模块化了。
可以大批量的使用。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-19 18:40 , Processed in 0.334461 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表