代码之家  ›  专栏  ›  技术社区  ›  Kevin Boyd

如何检查单元格是否有图片?

  •  5
  • Kevin Boyd  · 技术社区  · 14 年前

    在Excel中,我想检查特定单元格(例如“c12”)是否有图片?
    我怎么能这样做?

    4 回复  |  直到 9 年前
        1
  •  7
  •   GSerg    14 年前

    通过循环查看工作表的“形状”集合,查找 .TopLeftCell 与目标范围的地址相同。

        2
  •  2
  •   Bo Persson tox    13 年前

    我有一种情况,我想从工作表中选定的单元格中删除图片(在我的案例图表中),并保留其他图片,因此删除所有图片不是一个选项。我留下了一些调试和一些额外的代码来告诉用户发生了什么。

    Public Sub RemoveUnWantedGraphs()
    
        Dim shp As Shape
        Dim rangeToTest As Range
        Dim c As Range
        Dim shpList
    
        'Set the rangeToTest variable to the selected cells
        Set rangeToTest = Selection
    
        'Loop Over the the selected cells
        For Each c In rangeToTest
    
    
            'Inner loop to iterate over the shapes collection for the activesheet
            Set shpList = ActiveSheet.Shapes
            For Each shp In shpList
    
                Application.StatusBar = "Analysing:- " + c.Address + " Graphs To Find:- " & shpList.Count
    
    
                'If the address of the current cell and the address
                'of the shape are the same then delete the shape
                If c.Address = shp.TopLeftCell.Address Then
    
                    Debug.Print "Deleting :- " & shp.Name
                    shp.Delete
    
                    DoEvents
                End If
    
            Next shp
    
        Next c
    
        Application.StatusBar = ""
    
        MsgBox "All Shapes In Range Deleted"
    
    End Sub
    
        3
  •  2
  •   dprocter    9 年前

    最简单的解决方案是创建一个函数,如果图像存在于单元格中,则返回1;如果图像不存在,则返回0。这只适用于单个单元格,需要对多单元格范围进行修改。

    Function CellImageCheck(CellToCheck As Range) As Integer
    ' Return 1 if image exists in cell, 0 if not
    
        Dim wShape As Shape
    
        For Each wShape In ActiveSheet.Shapes
            If wShape.TopLeftCell = CellToCheck Then
                CellImageCheck = 1
            Else
                CellImageCheck = 0
            End If
        Next wShape
    
    End Function
    

    然后可以使用以下方法运行此代码:

    Sub testFunction()
    
        If CellImageCheck(Range("B6")) Then
            MsgBox "Image exists!"
        Else
            MsgBox "Image does not exist"
        End If
    
    End Sub
    
        4
  •  1
  •   Kent Lau    13 年前
    For Each wShape In ActiveSheet.Shapes
    If (wShape.Type <> 13) Then wShape.Delete ' If the shape doesn't represent a Picture,     ' delete
    Next wShape