簡體   English   中英

檢查Excel VBA中的一系列單元格中是否存在圖像

[英]Check if exists an image in a range of cells in Excel VBA

我有一個Excel文件,我要檢查其中是否存在一定范圍的單元格圖像。

我一直在嘗試以下代碼:

Sub findImage(Cell As Range)
   Dim Caddress As String
   Dim Pict As Excel.Picture
   Application.Volatile
   Sheets("Sheet1").Select
   Caddress = Cell.Address 'Assign the range
   For Each Pict In ActiveSheet.Pictures 'Check for each picture in the range
      If Pict.TopLeftCell.Address = Caddress Then 'if exists in the range shows a message
        MsgBox "The image exists!"
         Exit For 'break for
         Exit Sub 'break sub
      End If
   Next Pict
   MsgBox "NO", vbInformation 'if not exists shows a message
End Sub

該代碼不起作用,顯示錯誤“不兼容類型Err。13”。

有關評論的任何問題帖子。

我對代碼進行了一些調整(刪除了.Select行),並添加了haveImage布爾值,以使代碼更具邏輯性。 我還將Cell更改為Cel ,因為我發現使用Cell會與Cells混淆。

Sub findImage(Cel As Range)
Dim Caddress As String
Dim shp    As Shape
Dim haveImage As Boolean

haveImage = False

Application.Volatile
' Sheets("Sheet1").Activate
Caddress = Cel.Address      'Assign the range
For Each shp In Sheets("Sheet1").Shapes    'Check for each picture in the range
    If shp.Type = msoPicture Then
        If shp.TopLeftCell.Address = Caddress Then    'if exists in the range shows a message
            haveImage = True
            Exit Sub             'break sub
        End If
    End If
Next shp

If haveImage Then
    Exit Sub
Else
    MsgBox "NO", vbInformation    'if not exists shows a message
End If
End Sub

要使用它,您將這樣稱呼它: Call findimage(Sheets("Sheet1").Range("C12"))

注意:如果單元格具有具有畫面的左上角的圖片,在細胞中,它會返回“否”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM