Macro to Add Default Picture if Picture is not Detected

I want to be able to add a default picture when a macro is run if there is not a picture already present. I have an excel file that asks for an image and positions it within a cell. I have a button that inserts the picture and names it as "ImagePicture". Now, the picture is not mandatory to be added but I would still like to add a default picture of a symbol to serve as a placeholder.
bbarreneAsked:
Who is Participating?
 
kittenwhiskyConnect With a Mentor Commented:
this code checks if a shape called "ImagePicture" already exists, if not, it inserts a default picture and names it "ImagePicture"
Sub InsertDefaultPicture()

Dim ImagePicture As Picture, DefaultPicturePath As String

'Default picture path
'change as appropriate
DefaultPicturePath = "C:\Users\Public\Pictures\DefaultPicture.jpg"

'test if ImagePicture already exists in sheet
On Error Resume Next
    Set ImagePicture = ActiveSheet.Shapes("ImagePicture")
On Error GoTo 0

'if ImagePicture doesn't exist, insert default Picture
If ImagePicture Is Nothing Then
    Set ImagePicture = ActiveSheet.Pictures.Insert(DefaultPicturePath)
    ImagePicture.Name = "ImagePicture"
End If

End Sub

Open in new window

0
 
bbarreneAuthor Commented:
Thanks for the help.
0
 
bbarreneAuthor Commented:
I needed to change the first code below to the second code below in order for it to work.

Thanks for the help.


Dim ImagePath as Picture

Dim Image Path as Object

Open in new window

0
 
bbarreneAuthor Commented:
Whoops. I meant
Dim ImagePicture as Picture 

Dim ImagePicture as Object

Open in new window

0
All Courses

From novice to tech pro — start learning today.