Solved

Picture & VBA fun Fun FUn FUN :)

Posted on 2011-03-23
5
318 Views
Last Modified: 2012-06-27
Hello all,

I have a picture on a worksheet of a lego head labelled "lego_head".  I would like to select a range of cells and for all the cells within the chosen selection I would like to paste a copy of the lego head mentioned above.  I.e. If I select cells "B2:C4" I would expect to see 6 lego heads (one in each of the 6 cells).

Any help would be greatly appreciated.
0
Comment
Question by:Canders_12
  • 2
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
nutsch earned 400 total points
Comment Utility
Select your lego head, then run this code.

Sub copyPictureToRange()
Dim rg As Range, img As Shape, cl As Range, img2 As Shape

Set rg = Range("B2:C4")
Set img = ActiveSheet.Shapes(Selection.Name)

For Each cl In rg.Cells
    
    img.Copy
    ActiveSheet.Paste
    Set img2 = ActiveSheet.Shapes(Selection.Name)
    img2.Top = cl.Top
    img2.Left = cl.Left
    img2.Height = cl.Height
    If img2.Width > cl.Width Then img2.Width = cl.Width
    
Next cl

End Sub

Open in new window


Thomas
0
 

Author Comment

by:Canders_12
Comment Utility
nutsch:

Many thanks for the swift reply.  The only question I have is how easy is it to amend the code so teh range may be variable.  "B2:C4" was an example.  The user may choose "B2:D5".

I hope this makes sense? Many thanks.
0
 
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 100 total points
Comment Utility
This is only to support Thomas's solution.

Thomas chose to fix the target range and let the user select the picture. You can reverse this by changing line 5 (picture name instead of selection.name) and line 4 (selection.address instead of the given range in quotes)

Saqib
0
 

Author Closing Comment

by:Canders_12
Comment Utility
Many thanks to you both, greatly appreciated.
0
 
LVL 39

Expert Comment

by:nutsch
Comment Utility
Glad to help. Thanks for jumping in, Saqib.

Thomas
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now