返回
首页 > 儿童教育

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

时间: 2023-01-26

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

猜你喜欢

版权所有 Copyright©2023 餐饮美食网 版权所有

粤ICP备15109582号

联系邮箱:187348839@qq.com