excel怎么识别带图片的单元格

Q前不久有网友问我,在工作表的一列中,有些单元格放置有图片,有些单元格为空,如何识别带有图片的单元格并输入相应的文字?如所示,对工作表Sheet1的列B中含有图片的单元格,输入“有图片”,而没有图片的单元格则输入“无图片”,效果如列G所示。

excel怎么识别带图片的单元格

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属性来获取图片所在单元格的地址,然后与单元格相比较,以判断单元格中是否含有图片。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。