> 文章列表 > excel如何识别带有图片的单元格

excel如何识别带有图片的单元格

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