Add multiple pop up picture comment

BillyKit

New Member
Joined
Dec 14, 2009
Messages
11
I tried to add multiple pictures to multiple cells using the following add comment macro. It works perfectly when there are images in pictures1 folder that correspond to the values in the selected cells.There are instances where some images are not in the folder and the macro will return an error. What lines of code do I need to add to force it to skip those cells and continue to add comment to the next cell. Thank you.

Sub Addpic()
For Each cell In Selection
MyPic = "C:\Pictures1\" & cell.Value
With cell.AddComment
.Shape.Fill.UserPicture MyPic
.Shape.Height = 300
.Shape.Width = 300
End With
Next cell
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi and welcome to the board!!
Have you tried?
Code:
On Error Resume Next

lenze
 
Upvote 0
Or you could test that the picture actually exists in the directory:

Code:
[COLOR=blue]Public[/COLOR] [COLOR=blue]Function[/COLOR] ValidDir([COLOR=blue]ByVal[/COLOR] strPath [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]) [COLOR=blue]As[/COLOR] [COLOR=blue]Boolean[/COLOR]
    [COLOR=blue]If[/COLOR] [COLOR=blue]Len[/COLOR](strPath) = 0 [COLOR=blue]Then[/COLOR]
        ValidDir = [COLOR=blue]False[/COLOR]
        [COLOR=blue]Exit[/COLOR] [COLOR=blue]Function[/COLOR]
    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    ValidDir = [COLOR=blue]Len[/COLOR]([COLOR=blue]Dir$[/COLOR](strPath)) > 0
[COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
 
[COLOR=blue]Sub[/COLOR] Addpic()
    [COLOR=blue]For[/COLOR] [COLOR=blue]Each[/COLOR] cell [COLOR=blue]In[/COLOR] Selection
        MyPic = "C:\Pictures1\" & cell.Value
        [COLOR=blue]If[/COLOR] ValidDir(MyPic) [COLOR=blue]Then[/COLOR]
            [COLOR=blue]With[/COLOR] cell.AddComment
            .Shape.Fill.UserPicture MyPic
            .Shape.Height = 300
            .Shape.Width = 300
            [COLOR=blue]End[/COLOR] [COLOR=blue]With[/COLOR]
        [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    [COLOR=blue]Next[/COLOR] cell
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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