Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Picture & VBA fun Fun FUn FUN :)

Posted on 2011-03-23
5
Medium Priority
?
329 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 1600 total points
ID: 35201718
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
ID: 35201965
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 400 total points
ID: 35202099
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
ID: 35202145
Many thanks to you both, greatly appreciated.
0
 
LVL 39

Expert Comment

by:nutsch
ID: 35202380
Glad to help. Thanks for jumping in, Saqib.

Thomas
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 article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

885 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