+ Reply to Thread
Results 1 to 2 of 2

Inserting Multiple Images based on links??

Hybrid View

  1. #1
    Registered User
    Join Date
    10-01-2010
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007
    Posts
    6

    Inserting Multiple Images based on links??

    I'll make this simple. I have an Excel sheet with two columns (A & B) that contain paths to two different picture files saved on the local system. There are a total of 1,60,000 rows like this.
    I need a macro to:
    1. read the path in Column A and insert that picture in Column C
    2. read the path in Column B and insert that picture in Column D
    3. resize the cells to fit the pictures

    Any help will be highly appreciated since I didn't find a solution on the boards yet and I'm in a bit of a fix here working against a deadline. Thank you.

  2. #2
    Forum Contributor
    Join Date
    06-09-2011
    Location
    Germany
    MS-Off Ver
    Excel 2016
    Posts
    194

    Re: Inserting Multiple Images based on links??

    Hi Vivekmartin,

    the following macro should (almost) do what you need. The only differences is that rather than resizing the cells, the macro will first adjust the row height to a "standard heigth" (adjustable), and then resize the pictures to fit into those cells. As the runtime for the huge number of pictures in your example can be rather long, I would suggest to test this first with a limited amount of pictures.

    Option Explicit
    
    Sub PastePictures()
        Dim i As Double
        Dim StandardRowHeight As Double
        Dim Lastrow As Double
        Dim MaxwidthC As Double
        Dim MaxwidthD As Double
        Dim Shp As Shape
        Dim PicturePath As String
        Dim PicCounter As Double
    
    
        'Adjust your setting Here
        StandardRowHeight = 50
    
        '________________________________________________________
    
    
        Application.ScreenUpdating = False
        'delete old pictures first
        For Each Shp In ActiveSheet.Shapes()
            If Left(Shp.Name, 5) = "PicA_" Or Left(Shp.Name, 5) = "PicB_" Then
                Shp.Delete
            End If
        Next Shp
    
        Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
        For i = 2 To Lastrow
            ActiveSheet.Rows(i).EntireRow.RowHeight = StandardRowHeight
            'column A
            PicturePath = ActiveSheet.Cells(i, 1).Value
            If PicturePath <> Empty Then
                'Paste to column C
                Cells(i, 3).Select
                ActiveSheet.Pictures.Insert(PicturePath).Select
                With Selection
                    .Name = "PicA_" & i
                    'Fix Picture Size
                    .ShapeRange.LockAspectRatio = msoTrue
                    .ShapeRange.Height = 0.9 * StandardRowHeight
                    If .ShapeRange.Width > MaxwidthC Then MaxwidthC = .ShapeRange.Width
                    PicCounter = PicCounter + 1
                End With
            End If
            
            'column B
            PicturePath = ActiveSheet.Cells(i, 2).Value
            If PicturePath <> Empty Then
                'Paste to column D
                Cells(i, 4).Select
                ActiveSheet.Pictures.Insert(PicturePath).Select
                With Selection
                    .Name = "PicB_" & i
                    'Fix Picture Size
                    .ShapeRange.LockAspectRatio = msoTrue
                    .ShapeRange.Height = 0.9 * StandardRowHeight
                    If .ShapeRange.Width > MaxwidthD Then MaxwidthD = .ShapeRange.Width
                    PicCounter = PicCounter + 1
                End With
            End If
        Next i
        'Adjust column width to the biggest picture
        ActiveSheet.Columns(3).EntireColumn.ColumnWidth = MaxwidthC / 5.35
        ActiveSheet.Columns(4).EntireColumn.ColumnWidth = MaxwidthD / 5.35
        Application.ScreenUpdating = True
        MsgBox PicCounter & " Pictures inserted succesfully!"
    End Sub
    For your reference I have attached my sample workbook.

    Let me know if that is what you needed.

    Theo
    Remember To Do the Following....
    1. Upload sample files
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Inserting images based on an Excel list of items
    By Antiriad2097 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-06-2013, 01:08 PM
  2. inserting multiple images into spreadsheet
    By the_adam in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-23-2013, 10:06 AM
  3. MAcro for inserting multiple images into rectangles
    By Jagasama in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-03-2012, 10:47 AM
  4. Inserting images into a cell based on another cells value.
    By ccScotty in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-04-2012, 12:08 PM
  5. Excel 2007 : Inserting multiple images
    By Mickmac in forum Excel General
    Replies: 0
    Last Post: 03-08-2011, 05:14 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1