I'm using Excel 2007 currently, but for the purposes of this example, but I doubt I matters for something of this sort.
I found how to loop through all objects within the pictures collection for a specific worksheet, and I've done that, deleting all in a worksheet - just a copy/paste operation that was driving me bonkers.
However, my code (posted below) seems to run extremely slow - specifically, it runs slowest until it finds the first object. That made me wonder, is there a better way to do this, and specifically, can I narrow the scope to a set range somehow? I tried setting the Range, and looking at a Pictures collection for a range, but apparently one doesn't exist.
Here's my working code for looping through all pictures in a Worksheet:
Then I tried adapting the same code a couple of ways (one with a Range variable, the other just by referencing the a range within the worksheet. The latter is what is now showing in the code (non-working as of now)):
This code is honestly not very important at this time, but I wanted to use it as a learning experience. Can anybody offer any advice/suggestions/references/examples for better handling this particular operation? Surely there's a way for this to run faster.
--
"If to err is human, then I must be some kind of human!" -Me
I found how to loop through all objects within the pictures collection for a specific worksheet, and I've done that, deleting all in a worksheet - just a copy/paste operation that was driving me bonkers.
However, my code (posted below) seems to run extremely slow - specifically, it runs slowest until it finds the first object. That made me wonder, is there a better way to do this, and specifically, can I narrow the scope to a set range somehow? I tried setting the Range, and looking at a Pictures collection for a range, but apparently one doesn't exist.
Here's my working code for looping through all pictures in a Worksheet:
Code:
Private Sub RemovePictures()
[green]'Code programmed for removing all the pictures that come along with a specific copy/paste operation[/green]
Dim wb As Workbook
Dim ws As Worksheet
Dim p As Object [green]'pictures[/green]
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
For Each p In ws.Pictures
p.Select
[green]'MsgBox p.Name[/green]
p.Delete
Next p
Range("A1").Select
Set p = Nothing
Set ws = Nothing
Set wb = Nothing
MsgBox "Operation Complete! " & Chr(13) & "All pictures deleted!", vbInformation, "Finished!"
End Sub
Then I tried adapting the same code a couple of ways (one with a Range variable, the other just by referencing the a range within the worksheet. The latter is what is now showing in the code (non-working as of now)):
Code:
[green]'Try with a range:[/green]
Private Sub RemovePictures1()
Dim wb As Workbook
Dim ws As Worksheet
Dim p As Object 'pictures
Application.ScreenUpdating = True
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
For Each p In ws.Range("A1:B240").Pictures
p.Select
[green]'MsgBox p.Name[/green]
p.Delete
Next p
Range("A1").Select
Set p = Nothing
Set ws = Nothing
Set wb = Nothing
MsgBox "Operation Complete! " & Chr(13) & "All pictures deleted!", vbInformation, "Finished!"
End Sub
This code is honestly not very important at this time, but I wanted to use it as a learning experience. Can anybody offer any advice/suggestions/references/examples for better handling this particular operation? Surely there's a way for this to run faster.
--
"If to err is human, then I must be some kind of human!" -Me