You are here:

Excel/vba insert pics multiple times

Advertisement


Question
Hi Tom,
firstly, i want to thank you again for your help in the past!
just upgraded to Excel 2013.

this new project i am working on is to create labels based on an inventory # assigned.

the contents of the label is:
1. a picture (link to picture is column g)
2. sku# (column c)


so basically i want cell a1 to have a picture (top justified centered) and text bottom justified centered.  i tried copying and pasting the formula "=c1" to every row - is there a way to include this in the code?


here is a clip of the code being used to insert the picture.
Sub getpics()

Dim R As Range, cell As Range, pic As Variant
Dim g1 As Range
Set R = Range("g1:g10")

On Error Resume Next
For Each cell In R
If Len(Trim(cell)) > 0 Then
Set r1 = Cells(cell.Row, "A")
g1.Select
Set pic = ActiveSheet.Pictures.Insert(cell.Value)

With pic
.Top = r1.Top
 .Left = r1.Left
 .ShapeRange.LockAspectRatio = msoTrue
' .Height = r1.RowHeight
' .Width = r1.Width
' .Placement = xlMoveAndSize
End With

End If
Next
End Sub

thanks in advance for your time.
Eddie

Answer
Eddie,

I have to assume that column A is wider than your pictures and that the rows are taller than your picture height.  

Sub getpics()

Dim R As Range, cell As Range, pic As Variant
Dim g1 As Range, cell1 As Range, r1 As Range
Dim wcell As Long, wpic As Long
Set R = Range("g1:g10")
Set g1 = Range("G1")
On Error Resume Next
For Each cell In R
If Len(Trim(cell)) > 0 Then
Set r1 = Cells(cell.Row, "A")
g1.Select
Set pic = ActiveSheet.Pictures.Insert(cell.Value)
cell1 = cell.Offset(0, -6)  ' column A for the row of Cell
With pic
.Top = r1.Top
  wcell = cell1.Width
  wpic = .Width
  
  .Left = cell1.Left + (wcell - wpic) / 2
' .Left = r1.Left
 .ShapeRange.LockAspectRatio = msoTrue
' .Height = r1.RowHeight
' .Width = r1.Width
' .Placement = xlMoveAndSize
End With
cell1.Formula = "=" & cell.Offset(0, 2).Address(0, 0)
cell1.HorizontalAlignment = xlCenter
cell1.VerticalAlignment = xlBottom
End If
Next
End Sub

Untested, but I believe the changes I made to your code will do what you want. (I am assuming the code you show loops through the cells in G1:G10 and brings in the appropriate picture.

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.