?
Solved

Excel vba automation: Assign macro to imported graphic

Posted on 2014-11-11
7
Medium Priority
?
374 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 34

Accepted Solution

by:
Norie earned 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

762 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