You are here:

Excel/Inserting multiple pictures

Advertisement


Question
Hi Tom,

In 2009, you posted the following code in response to a question regarding inserting multiple pictures in Excel 2007. I am trying to insert multiple pictures in  Excel 2010 but nothing happens when I run your code.Any suggestions you can offer would be greatly appreciated. Thanks.

Sub ProcessFiles()
Dim sPath As String, s As String, r As Range
Dim shp As ShapeRange
Dim c As Range, cell As Range, sname As String
Dim p As Picture, diffwidth As Double, diffHeight As Double
sPath = "C:UsersPublicPicturesSample Pictures\"
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
Set r = Range("B2", Cells(Rows.Count, 2).End(xlUp))
For Each cell In r
 cell.Offset(0, 1).Select
 Set c = cell.Offset(0, -1)
 s = sPath & cell.Value & ".jpg"  'remove the .jpg if the cell contains the extension
 sname = Dir(s)
 If sname <> "" Then
   Set p = ActiveSheet.Pictures.Insert(s)
   Set shp = p.ShapeRange
   diffwidth = c.Width - p.Width
   If diffwidth > 0 Then
      p.Left = c.Left + 0.5 * diffwidth
   Else
      p.Left = c.Left
   End If

  diffHeight = c.Height - p.Height
   If diffHeight > 0 Then
      p.Top = c.Top + 0.5 * diffHeight
   Else
      p.Top = c.Top
   End If
 End If
Next
   
End Sub

Answer
before macro
before macro  

after macro
after macro  
George,

I fixed the path (used the same as you)

Sub ProcessFiles()
Dim sPath As String, s As String, r As Range
Dim shp As ShapeRange
Dim c As Range, cell As Range, sname As String
Dim p As Picture, diffwidth As Double, diffHeight As Double
sPath = "C:\Users\Public\Pictures\Sample Pictures\"
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
Set r = Range("B2", Cells(Rows.Count, 2).End(xlUp))
For Each cell In r
 cell.Offset(0, 1).Select
 Set c = cell.Offset(0, -1)
 s = sPath & cell.Value & ".jpg"  'remove the .jpg if the cell contains the extension
 sname = Dir(s)
 If sname <> "" Then
   Set p = ActiveSheet.Pictures.Insert(s)
   Set shp = p.ShapeRange
   diffwidth = c.Width - p.Width
   If diffwidth > 0 Then
      p.Left = c.Left + 0.5 * diffwidth
   Else
      p.Left = c.Left
   End If

 diffHeight = c.Height - p.Height
   If diffHeight > 0 Then
      p.Top = c.Top + 0.5 * diffHeight
   Else
      p.Top = c.Top
   End If
 End If
Next
   
End Sub

put in five file names (with no extension)  as seen in the picture

and it brought the pictures in - No Problem

hopefully the attached images will show success

--
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.