Avatar of AFGPHXExcel
AFGPHXExcel

asked on 

How do I get Excel VBA to identify a shape as a picture?

I am asking Excel to count shapes on a worksheet and delete them. The goal is to delete buttons, but it is also a deleting a picture. Is it possible to differentiate between the buttons and picture during its count? The code is as follows:

iCount = ActiveSheet.Shapes.Count
            For i = iCount To 1 Step -1
               
                ActiveSheet.Shapes(i).Delete
            Next i

Thanks!
Microsoft Excel

Avatar of undefined
Last Comment
AFGPHXExcel
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Check the button typename, should catch all and leave validation ranges.

If oleobject then perhaps you will need oleobject but check if thT AFFECTS YOUR PICTURES USING A COPY FIRST.

CHRIS
Sub delShapes()
Dim sh As Object
Dim obj As Object
For Each sh In ActiveWorkbook.Sheets
    
    For Each obj In sh.Shapes
        Select Case TypeName(obj.DrawingObject)
            Case "Button", "OLEObject"
                obj.Delete
            Case Else
        End Select
    Next obj
Next sh

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of AFGPHXExcel
AFGPHXExcel

ASKER

Thanks!
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo