Changing Images With VBA

Coach3131

Board Regular
Joined
Nov 23, 2007
Messages
242
I am trying to write a macro where I can change images for a picture file. I can do the insert method, but if I change pictures and run it again, it just inserts the next picture on top of it instead of changing the current image. Can anyone help here?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
If I understand this right, you want to change from Orange to Blue.
If that is wrong, you'll have to explain a little more (for me anyway).
 
Upvote 0
I have a cell with a URL referenced that inserts an image. For example, www.mysite.com/image1.jpg.

The next time I run it, I may want to reference /image2.jpg. I want the picture to be in the same spot in the report as before but just changed based on the cell reference.

If I use the Insert method, it will just insert the image on top of the old one instead of change it out.
 
Upvote 0
Code:
Sub DeletePics()
    Dim **** As Shape
    For Each **** In Sheet1.Shapes
        If ****.Type = msoPicture Then ****.Delete
    Next

   'Here the rest of your code

End Sub

If you have some stray pictures floating around, it will delete them also.

Note: It will delete all pictures
 
Upvote 0
Code:
ImageReference = Range("MyImageReference").Value
On Error Resume Next
ActiveSheet.Shapes.Range(Array("MyImagePic")).Delete
Range("ImageLocation").Select
ActiveSheet.Pictures.Insert(ImageReference).Name = "MyImagePic"

I am able to insert and set location this way... there may be a better way to do the error handling so that if a pic doesn't exist, it doesn't break the code. Also, the code above to delete images, is there a way to put a list of the images to change in an array versus deleting every image? Lets say that I have 5 images on a page and 2 are always static... I would only want to delete the 3 that are not static and replace their references versus delete every picture.
 
Last edited:
Upvote 0
I think you could give the static pictures a name property and use an if statement to say if ****.name <> "name" then ****.Delete
 
Upvote 0
The code jolivanes gave is providing an object not found error even though I have an image on a page that I am testing the code with.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top