Solved

Excel vba automation: Assign macro to imported graphic

Posted on 2014-11-11
7
333 Views
Last Modified: 2014-11-11
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
0
Comment
Question by:Kelvin4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 40435651
Can't you just use this?
 ActiveSheet.Shapes(ActiveSheet.Shapes.Count).OnAction = "tst"

Open in new window

0
 

Author Comment

by:Kelvin4
ID: 40435667
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..
0
 

Author Comment

by:Kelvin4
ID: 40435792
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 40435866
The code I posted would assign a macro to the most recently added shape, it would go straight after the code you posted.

If you had something like this sh would be a reference to the newly added picture.
Dim sh As Shape
set sh = ActiveSheet.Shapes.AddPicture "path\x.jpg", msoCTrue, msoTrue, .left, .top, -1, -1

Open in new window

With that reference you could assign a macro like this.
sh.OnAction = "TheMacroToAssign"

Open in new window

0
 

Author Closing Comment

by:Kelvin4
ID: 40436193
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
0
 
LVL 33

Expert Comment

by:Norie
ID: 40436326
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

0
 

Author Comment

by:Kelvin4
ID: 40436553
ok, thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question