Solved

Excel vba automation: Assign macro to imported graphic

Posted on 2014-11-11
7
351 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 34

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 34

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 34

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

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!

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

695 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