This post will guide you how to check if a specific cell contains an image or picture in excel. Is it possible to check if a cell contains an image. This post will guide you how to define a User Defined Function to determine if an image exists in a specified cell. Or how to use VBA Macro code to check if a cell contains an image.
Table of Contents
1. Check If a Cell contain Image Using User Defined Function
The simplest method is to create a user defined function to check if a cell contains an image file, and return 1 if image exists in cell. Otherwise, returns 0. You can follow these steps:
Step1: open your excel workbook and then click on “Visual Basic” command under DEVELOPER Tab, or just press “ALT+F11” shortcut.
Step2: then the “Visual Basic Editor” window will appear.
Step3: click “Insert” ->”Module” to create a new module
Step4: paste the below VBA code into the code window. Then clicking “Save” button.
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
Step5: back to the current worksheet, try to enter the below formula in Cell D6.
=CellImageCheck(B6)
2. Check If a Cell contain Image with VBA Macro
You can also use the following VBA code to check if a cell contains an image. You just need to repeat the above 1-3 steps firstly, then continue to do the following steps:
Step1: paste the below VBA code into the code window. Then clicking “Save” button.
Sub CellImageCheck()
Dim checkRange As Range
Dim x As Shape
Dim flag As Boolean
On Error Resume Next
Set checkRange = Application.InputBox("please enter one cell that you want to check", "CellImageCheck", Selection.Address, , , , , 8)
If checkRange Is Nothing Then Exit Sub
flag = False
For Each x In ActiveSheet.Shapes
If x.TopLeftCell.Address = checkRange.Address Then
flag = True
End If
Next
If flag Then
MsgBox "Found an image!"
Else
MsgBox "No Image"
End If
End Sub
Step2: back to the current worksheet, then run the above excel macro. Click Run button.
Step3: select one cell that you want to check, such as: B6
Step4: Let’s see the result:
3. Video: Check If a Cell contain Image in Excel
This video demonstrates how to use a User Defined Function with VBA Macro to check if a cell contains an image or picture in Excel.
Leave a Reply
You must be logged in to post a comment.