Delete Pictures inside worksheets using VBA

0

Paste the code to Module 1 then F5 to lunch then click run



Option Explicit

Sub DeletePictures()

    On Error GoTo ErrorHandler


    Dim shp As Shape

    Dim hasPictures As Boolean

    

    'Check if there are any pictures in the sheet

    For Each shp In ActiveSheet.Shapes

        If Left(shp.Name, 7) = "Picture" Then

            hasPictures = True

            Exit For

        End If

    Next shp

    

    'If there are pictures, delete them

    If hasPictures Then

        For Each shp In ActiveSheet.Shapes

            If Left(shp.Name, 7) = "Picture" Then

                shp.Delete

            End If

        Next shp

    Else

        MsgBox "No pictures were found in the sheet."

    End If

    

    Exit Sub

    

ErrorHandler:

    MsgBox "An error occurred: " & Err.Description

End Sub


Post a Comment

0Comments

"Please keep your comments respectful and on-topic."
"Your email address will not be published."
"HTML tags are not allowed in comments."
"Spam comments will be deleted."

Post a Comment (0)