Link to home
Start Free TrialLog in
Avatar of Roger
RogerFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Excel vba automation: Assign macro to imported graphic

I assign a macro to a graphic that I have imported via xl_Ribbon/Insert/Picture :
    ActiveSheet.Shapes.Range(Array("Picture 3")).Select
    Selection.OnAction = "tst"

BUT, if I import a graphic to an Excel cell, how do I identify that graphic so I can use vba to assign a macro to it?

With Range(Cells(rn.row + 1, 7), Cells(rn.row + 1, 1))
   ActiveSheet.Shapes.AddPicture "path\x.jpg", msoCTrue, msoTrue, .left, .top, -1, -1
   ActiveSheet.Shapes(ActiveSheet.Shapes.Count).height = .height
End With

If you could briefly explain the principle of connecting input file name with
Shapes.Range, it would help me!

Thanks in anticipation
Kelvin
Avatar of Norie
Norie

Can't you just use this?
 ActiveSheet.Shapes(ActiveSheet.Shapes.Count).OnAction = "tst"

Open in new window

Avatar of Roger

ASKER

Not sure!
The sheet will contain potentially hundreds of rows with graphic icons, but each icon is added at a separate time, as new data rows are appended. I only need to assign the macro (once) to each new data row.

Also, what If I dont want that macro assigned to certain shapes on the ActiveSheet?
Thanks..
Avatar of Roger

ASKER

would I  be right in thinking that if I could:
Dim sh As Shape
set sh = ActiveSheet.Shapes.AddPicture "path\x.jpg", msoCTrue, msoTrue, .left, .top, -1, -1

then sh.name would identify the shape for the assignment of the macro?

Kelvin
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Roger

ASKER

The text & code you wrote:
"Can't you just use this?
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).OnAction = "tst"     "

Kelvin then proposed what you claim to be the correct answer:
set sh = ActiveSheet.Shapes.AddPicture "path\x.jpg", msoCTrue, msoTrue, .left, .top, -1, -1

then sh.name would identify the shape for the assignment of the macro?

You then write
"The code I posted would assign a macro to the most recently added shape, it would go straight after the code you posted. "

I dont think so, it would have assigned the macro to all shapes on active page!!

But thanks for the contribution.
Kelvin
Kelvin

Not sure what you mean.

The first code I posted would only assign the macro to the most recent picture added.

This returns one shape/picture - the last member of the Shapes collection.
  ActiveSheet.Shapes(ActiveSheet.Shapes.Count)

Open in new window

Avatar of Roger

ASKER

ok, thanks