Solved

Picture & VBA fun Fun FUn FUN :)

Posted on 2011-03-23
5
319 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
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 100 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

777 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