简体   繁体   English

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

[英]How to check if a cell has a picture?

In Excel, I want to check if a specific cell for instance "C12" has a picture?在 Excel 中,我想检查例如“C12”的特定单元格是否有图片?
How could I do this?我怎么能这样做?

为此,您可以循环访问工作表的 Shapes 集合,查找.TopLeftCell与目标范围具有相同地址的形状。

The simplest solution is to create a function that will return 1 if image exists in cell, 0 if it does not.最简单的解决方案是创建一个函数,如果单元格中存在图像,则返回 1,否则返回 0。 This only works for individual cells and needs modified for multi-cell ranges.这仅适用于单个单元格,需要针对多单元格范围进行修改。

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

This code can then be run using:然后可以使用以下代码运行此代码:

Sub testFunction()

    If CellImageCheck(Range("B6")) Then
        MsgBox "Image exists!"
    Else
        MsgBox "Image does not exist"
    End If

End Sub

I had a situation where I wanted to delete pictures (In my case charts) from selected cells on a worksheet and leave others in place therefore removing all pictures was not an option.我有一种情况,我想从工作表上的选定单元格中删除图片(在我的案例图表中)并将其他单元格留在原地,因此删除所有图片不是一个选项。 I've left behind some debugging and also some extra code to tell the user what is going on.我留下了一些调试和一些额外的代码来告诉用户发生了什么。

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

This is quite an old thread so don't know whether my post will help anybody, but I encountered a similar problem today and after some thinking, derived solution.这是一个很老的帖子,所以不知道我的帖子是否对任何人有帮助,但我今天遇到了类似的问题,经过一些思考,得出了解决方案。

I have first stored all range addresses where object exists, to an array and then in the second part of the code, checked each cell address in my selected range for the object against each element in array and carried out execution of tagging to an offset cell if array element address matches active cell address in selected range.我首先将对象存在的所有范围地址存储到一个数组中,然后在代码的第二部分中,根据数组中的每个元素检查对象的所选范围中的每个单元格地址,并执行对偏移单元格的标记如果数组元素地址与所选范围内的活动单元格地址匹配。 Hope, it helps.希望能帮助到你。 Here is the code:这是代码:

Option Explicit
Sub tagging()
Dim rng As Range, shp As Shape, n As Integer, arr() As String, m As Integer, arrm As Variant
m = 1
n = ActiveSheet.Shapes.Count
ReDim arr(n)
For Each shp In ActiveSheet.Shapes
    arr(m) = shp.TopLeftCell.Address
    m = m + 1
Next
   For Each rng In Selection
       m = 1
       For Each arrm In arr
           If rng.Address = arr(m) Then
              rng.Offset(0, 30).Value = "Yes"
              Exit For
           Else
              rng.Offset(0, 30).Value = "No"
           End If
                If m < n Then
                   m = m + 1
                Else
                   Exit For
                End If
      Next
  Next
End Sub
For Each wShape In ActiveSheet.Shapes
If (wShape.Type <> 13) Then wShape.Delete ' If the shape doesn't represent a Picture,     ' delete
Next wShape

Juhi's approach helped me. Juhi 的方法帮助了我。 I think there's an implied need in the original question to apply this to multiple cells or a contiguous range or even a whole sheet.我认为原始问题中隐含需要将其应用于多个单元格或连续范围甚至整个工作表。 In such a case, it's desirable not to consider each cell separately and loop through every shape in the sheet repeatedly for all the cells of interest.在这种情况下,最好不要单独考虑每个单元格,而是针对所有感兴趣的单元格重复遍历工作表中的每个形状。

I've changed the functionality a little to remove the nested loop and enter text into all the cells that contain a shape.我稍微更改了功能以删除嵌套循环并将文本输入到包含形状的所有单元格中。 This is optimised for my immediate need where the source data is a 4x40 cell region where the cells either contain a Shape or nothing at all.这是针对我的直接需求进行了优化,其中源数据是一个 4x40 单元格区域,其中单元格要么包含形状,要么根本不包含。 My method doesn't enter 'no' for the cells that contain no shape, but it's easy to enter that into the blank cells at the end.我的方法不会为不包含形状的单元格输入“否”,但很容易将其输入到最后的空白单元格中。

Sub MarkCellsWithShapes()

    Dim rng As Range, shp As Shape, n As Integer, arr() As String, m As Integer, arrm As Variant

    n = ActiveSheet.Shapes.Count
    ReDim arr(n)
    
    m = 1

    For Each shp In ActiveSheet.Shapes
        arr(m) = shp.TopLeftCell.Address
        Range(arr(m)) = "Yes"
        m = m + 1
    Next

End Sub

If you need to work in a specific range rather than a whole sheet, you could make the 'yes' instruction conditional (see VBA test if cell is in a range for tips on that).如果您需要在特定范围内工作而不是在整个工作表中工作,您可以将“是”指令设为有条件的(请参阅VBA 测试是否单元格在范围内以获取有关提示)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM