excel怎么識(shí)別帶圖片的單元格
Q:前不久有網(wǎng)友問(wèn)我,在工作表的一列中,有些單元格放置有圖片,有些單元格為空,如何識(shí)別帶有圖片的單元格并輸入相應(yīng)的文字?如所示,對(duì)工作表Sheet1的列B中含有圖片的單元格,輸入“有圖片”,而沒(méi)有圖片的單元格則輸入“無(wú)圖片”,效果如列G所示。

A:下面的代碼可以完成我們的需求:
Sub DecidePic()
Dim cell As Range
Dim lngCells As Long
Application.ScreenUpdating = False
‘設(shè)置查找列的單元格數(shù)
lngCells = 3
For Each cell In Range(“B2:B”& lngCells)
If PicIfExists(Sheet1, cell) Then
cell.Value = “有圖片”
Else
cell.Value = “無(wú)圖片”
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
在自定義函數(shù)PicIfExists中,使用Shape對(duì)象的TopLeftCell屬性來(lái)獲取圖片所在單元格的地址,然后與單元格相比較,以判斷單元格中是否含有圖片。






