- ·上一篇教育:excel2019函数公式之SERIES公式
- ·下一篇教育:如何将excel2019数据其转换为正方形?
excel2019如何识别带有图片的单元格?
Q:前不久有网友问我,在工作表的一列中,有些单元格放置有图片,有些单元格为空,如何识别带有图片的单元格并输入相应的文字?如所示,对工作表Sheet1的列B中含有图片的单元格,输入“有图片”,而没有图片的单元格则输入“无图片”,效果如列G所示。
A:下面的代码可以完成我们的需求:
Sub DecidePic()
Dim cell As Range
Dim lngCells As Long
Application.ScreenUpdating = False
‘设置查找列的单元格数
lngCells = 3
For Each cell In Range(“B2:B”& lngCells)
If PicIfExists(Sheet1, cell) Then
cell.Value = “有图片”
Else
cell.Value = “无图片”
End If
Next cell
Application.ScreenUpdating = True
End Sub
Function PicIfExists(wks As Worksheet, rng As Range) As Boolean
Dim shp As Shape
For Each shp In wks.Shapes
If shp.TopLeftCell.Address =rng.Address Then
PicIfExists = True
Exit For
End If
Next shp
End Function
在自定义函数PicIfExists中,使用Shape对象的TopLeftCell属性来获取图片所在单元格的地址,然后与单元格相比较,以判断单元格中是否含有图片。
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
