Solved

VBA code to delete a picture in a range

Posted on 2013-01-09
6
1,840 Views
Last Modified: 2013-01-09
Could you get me VBA code to delete pictures in Range("X1:AC4") in excel 2003? thanks,
0
Comment
Question by:HemlockPrinters
  • 5
6 Comments
 
LVL 26

Expert Comment

by:redmondb
ID: 38760934
Hi, HemlockPrinters.

pictures in Range("X1:AC4")
A picture is in the range if...
(1) ...any part of it is in the range.
(2) ...all of it is in the range.
(3) ...the top left corner is in the range.
(4) Something else?

Thanks,
Brian.
0
 

Author Comment

by:HemlockPrinters
ID: 38761011
all of it is in the range. thanks Brian.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38761013
HemlockPrinters,

The code below uses (1) - let me know if you want that changed...
Sub Delete_Pictures_in_Range()
Dim xShape    As Shape
Dim xCount    As Long
Dim xResponse As Long

xResponse = MsgBox("About to delete all pictures in the range X1:AC in the active sheet (" & ActiveSheet.name & ")." _
                & Chr(10) & "'OK' to continue, 'Cancel' to terminate.", vbOKCancel, "Delete_Pictures_in_Range")
If xResponse = 2 Then
    MsgBox ("User chose to cancel - run terminating.")
    Exit Sub
End If

Application.ScreenUpdating = False

    For Each xShape In ActiveSheet.Shapes
        If xShape.Type = msoPicture Then
            If Not Intersect(Range("X1:AC4"), Range(xShape.TopLeftCell, xShape.BottomRightCell)) Is Nothing Then
                xCount = xCount + 1
                Debug.Print xCount & " - " & xShape.name & " - " & xShape.TopLeftCell.Address & " - " & xShape.BottomRightCell.Address
                xShape.Delete
            Else
                Debug.Print "** - " & xShape.name & " - " & xShape.TopLeftCell.Address & " - " & xShape.BottomRightCell.Address
            End If
        End If
    Next
    
Application.ScreenUpdating = True

MsgBox xCount & " picture(s) deleted."

End Sub

Open in new window

Regards,
Brian.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Expert Comment

by:redmondb
ID: 38761023
Apologies, HemlockPrinters, crossing posts. I'll make the changes now.

Regards,
Brian.
0
 
LVL 26

Accepted Solution

by:
redmondb earned 500 total points
ID: 38761064
HemlockPrinters,

Please see below...
Sub Delete_Pictures_in_Range()
Dim xShape    As Shape
Dim xCount    As Long
Dim xResponse As Long
Dim xNot      As Boolean

xResponse = MsgBox("About to delete all pictures in the range X1:AC4 in the active sheet (" & ActiveSheet.name & ")." _
                & Chr(10) & "'OK' to continue, 'Cancel' to terminate.", vbOKCancel, "Delete_Pictures_in_Range")
If xResponse = 2 Then
    MsgBox ("User chose to cancel - run terminating.")
    Exit Sub
End If

Application.ScreenUpdating = False

    For Each xShape In ActiveSheet.Shapes
        If xShape.Type = msoPicture Then
            xNot = False
            If Not Intersect(Range("X1:AC19"), Range(xShape.TopLeftCell, xShape.BottomRightCell)) Is Nothing Then
                If Range(xShape.TopLeftCell, xShape.BottomRightCell).Address _
                = Intersect(Range("X1:AC19"), Range(xShape.TopLeftCell, xShape.BottomRightCell)).Address Then
                    xCount = xCount + 1
                    Debug.Print xCount & " - " & xShape.name & " - " & xShape.TopLeftCell.Address & " - " & xShape.BottomRightCell.Address
                    xShape.Delete
                Else
                    xNot = True
                End If
            Else
                xNot = True
            End If
            If xNot Then Debug.Print "** - " & xShape.name & " - " & xShape.TopLeftCell.Address & " - " & xShape.BottomRightCell.Address
        End If
    Next
    
Application.ScreenUpdating = True

MsgBox xCount & " picture(s) deleted."

End Sub

Open in new window

Regards,
Brian.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38761145
Thanks, HemlockPrinters.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Fixing a embedded format 7 29
Freeze Panes Solution 6 28
Excel Conditional Formatting in a Macro 4 25
Excel 2010 - Populate a formula in multiple cells 6 23
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now