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

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

office办公软件入门基础教程 » excel如何识别带有图片的单元格?

本文链接:【excel如何识别带有图片的单元格?】https://www.officeapi.cn/89526.html

OFFICE,天天学一点,进步多一点

联系我们