How to check if a shape or image exists in an active Excel sheet?


Usually we are not required to manipulate photographs or images within Excel; yet, judging by the volume of Google searches for this issue, it is evident that people are interested in learning more about how to do so. Creating a user-defined function to verify whether a cell includes an image file is the quickest and easiest technique to accomplish this.

This tutorial will walk you through the steps of defining a User Defined Function so that you may check to see if a picture is stored in a particular cell. Or how to use VBA Macro code to determine whether or not a cell has an image stored within it.

VBA Code to Check If an Image Exists or Not

In our example, we have an image in Excel, as shown in the following screenshot.

Step 1

Press Alt and F11 key (Alt+F11) to open Microsoft Visual Basic for Application windows. See the below given image.

Step 2

After that, select Insert > Module from the menu bar to bring up the popup Module window. See the below given image.

Step 3

After opening the Module Window, type the following VBA code in it.

Sub CheckImage()
   Dim xChar As Picture
   Dim xFlag As Boolean
   Dim xCharName As String
   On Error Resume Next
   Application.ScreenUpdating = False
   xCharName = "Picture 2"
   xFlag = False
   For Each xChar In ActiveSheet.Pictures
   Debug.Print xChar.Name
      If xChar.Name = xCharName Then
         MsgBox "The Image Exists", vbInformation, "VBOutput"
         xFlag = True
         Exit For
      End If
   Next
   If Not xFlag Then
      MsgBox "The Image does not Exists", vbInformation, "VBOutput"
   End If
   Application.ScreenUpdating = True
End Sub

Refer to the following screenshot.

In this VBA code in the place of xCharName=”Picture 2”, you can add your image name.

Step 4

After adding the VBA code, run the code by pressing F5 or Click Run.

You can save the above VBA code. To save the VBA code, go to File>Save.

After running the VBA code, if the image exists in the excel sheet, it will display “The Image Exists”.

Conclusion

In this tutorial, we explained how you can use a VBA code to check if an image exists or not in an active Excel sheet.

Updated on: 08-Feb-2023

543 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements