+ Reply to Thread
Results 1 to 23 of 23

VBA Picture insert from directoy in merged cell and scaling

Hybrid View

  1. #1
    Registered User
    Join Date
    08-25-2017
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    19

    VBA Picture insert from directoy in merged cell and scaling

    I have a worksheet with a few cells merged together (cells F12:H14). In these merged cells I want to automatically load a picture based on cell value in cell "I13".

    Location of pictures:
    The pictures are *.jpg files within the folder "pictures" which is in the same folder as the excel file. So the path must be relative.

    Loading:
    The picture must be loaded automatically when the worksheet is opened.

    Sizing:
    The picture must be autmatically sized to the size of the merged cells (F12:H14) and as both horizontally and vertically centered.

    Duplication:
    The sheet with the picture insert will automatically be duplicated based on the number of rows in a "data" sheet. The multiplied sheets gets automatically renamed to the cell values of the rows in de "data" sheet. So I guess that has someting to do with naming or placement of the VBA code?

    I cannot find any VBA that fullfills my needs (as ever ). Can someone help me with this.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Picture insert from directoy in merged cell and scaling

    See how next code can help
    To put in the sheet's code
    The range where to put the picture has been named: MyPicture
    See file attached
    Option Explicit
    Private Sub Worksheet_Activate()
    Dim WkPath As String
    WkPath = ActiveWorkbook.Path & "\" & "Pictures"
     
    Dim PicRg  As Range
        Set PicRg = Range("MyPicture")
    Dim WkPicN As String
    WkPicN = Range("I13")
    Dim WkPic As Picture
    
    Dim WkRg As Range
        Application.ScreenUpdating = False
        With ActiveSheet
            For Each WkPic In .Pictures
                Set WkRg = .Range(WkPic.TopLeftCell.Address & ":" & WkPic.BottomRightCell.Address)
                If Not Intersect(WkRg, PicRg) Is Nothing Then WkPic.Delete
            Next
        End With
    
        
        Set WkPic = ActiveSheet.Pictures.Insert(WkPath & "\" & WkPicN & ".jpg")
    '   Resize picture
        With WkPic
            .ShapeRange.LockAspectRatio = msoFalse
            .Top = PicRg.Top
            .Left = PicRg.Left
            .Width = (PicRg.Cells(1, 1).Width + PicRg.Cells(1, 2).Width + PicRg.Cells(1, 3).Width)
            .Height = (PicRg.Cells(1, 1).Height + PicRg.Cells(2, 1).Height + PicRg.Cells(3, 1).Height)
        End With
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    Last edited by PCI; 02-13-2020 at 06:59 PM.
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    08-25-2017
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    19

    Re: VBA Picture insert from directoy in merged cell and scaling

    Thanks PCI!

    That helps a lot. I tested it and 3 things which doesn't fullfill my needs:

    1. Sizing of the pictures: the pictures are streched out to the whole size of the cellrange (MyPicture). The picture scaling must be set to "aspect ratio". Max. size of the picture must be appr. 95% of the height of cellrange ("MyPicture);

    2. Location of the picture must be centered in "Mypicture";

    3. The picture must be loaded/updated automatically when the cell value I13 changes. Now the picture changes after switching the worksheet.

    I added some sample pictures to the example.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Picture insert from directoy in merged cell and scaling

    A remake
    in a module put
    Option Explicit
    
    Sub Treat()
    Dim WkPath As String
    WkPath = ActiveWorkbook.Path & "\" & "Pictures"
     
    Dim PicRg  As Range
        Set PicRg = Range("MyPicture")
    Dim WkPicN As String
    WkPicN = Range("I13")
    Dim WkPic As Picture
    Dim H, W
    
    Dim WkRg As Range
        Application.ScreenUpdating = False
        With ActiveSheet
            For Each WkPic In .Pictures
                Set WkRg = .Range(WkPic.TopLeftCell.Address & ":" & WkPic.BottomRightCell.Address)
                If Not Intersect(WkRg, PicRg) Is Nothing Then WkPic.Delete
            Next
        End With
    
        
        Set WkPic = ActiveSheet.Pictures.Insert(WkPath & "\" & WkPicN & ".jpg")
    '   Resize picture
        With WkPic
            .ShapeRange.LockAspectRatio = msoTrue
             W = PicRg.Cells(1, 1).Width + PicRg.Cells(1, 2).Width + PicRg.Cells(1, 3).Width
             H = PicRg.Cells(1, 1).Height + PicRg.Cells(2, 1).Height + PicRg.Cells(3, 1).Height
            .Height = 0.95 * H
            .Left = PicRg.Left + (W - .Width) / 2
            .Top = PicRg.Top + (H - .Height) / 2
            
     '       .Width = (PicRg.Cells(1, 1).Width + PicRg.Cells(1, 2).Width + PicRg.Cells(1, 3).Width)
     '       .Height = (PicRg.Cells(1, 1).Height + PicRg.Cells(2, 1).Height + PicRg.Cells(3, 1).Height)
        End With
        Application.ScreenUpdating = True
    
    End Sub
    in the sheet's code put


    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If (Target.Address <> Range("I13").Address) Then Exit Sub
        Call Treat
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    08-25-2017
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    19

    Re: VBA Picture insert from directoy in merged cell and scaling

    Thanks PCI! The example works fine, but with implementing in my workbook there are some difficulties:

    In I13 I placed a Vlookup formula which gets the cellvalue from a worksheet "Data". Than the picture will not load.
    See example.

    And when no picture refenece is placed in column B from the sheet "Data" the picture must remain empty, with no fault messages.

    Can I make a tweek or can you change the VBA code?
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Picture insert from directoy in merged cell and scaling

    In I13 I placed a Vlookup
    So you want to trigger the macro when calculation is done for I3 ??
    There is an event linked to calculation but no reference to the cell updated.
    A possibility could be to trigger when a selection is done ( what ever the cell) just to refresh the picture, is it OK ?

  7. #7
    Registered User
    Join Date
    08-25-2017
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    19

    Re: VBA Picture insert from directoy in merged cell and scaling

    See attachment, this is how I exactly have it in my workbook.

    - L13 automacically gets the name of the worksheet;
    - K13 reads the value of L13 (a tweek because otherwise the Vloopup didn' t work in the sheet);
    - I13 looks up the value of K13 in the sheet "Data" and gives back the value of column B in the sheet "Data"

    The macro must be triggered by opening of the worksheet.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Picture insert from directoy in merged cell and scaling

    The macro must be triggered by opening of the worksheet.
    For each sheet use

    Option Explicit
    Private Sub Worksheet_Activate()
        Call Treat
    End Sub

  9. #9
    Registered User
    Join Date
    08-25-2017
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    19

    Re: VBA Picture insert from directoy in merged cell and scaling

    Ok thank you, that did it!

    One last thing: when no reference is made to a picture I get an error message (see pict). Can you put a line of code in the vba that when there is no value in I13 the macro stops.

    Attachment 662750

  10. #10
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Picture insert from directoy in merged cell and scaling

    Change
        Dim WkPicN As String
        WkPicN = Range("I13")

    by

    Dim WkPicN As String
        WkPicN = Range("I13")
        If (Len(WkPicN) = 0) Then exitSub

  11. #11
    Registered User
    Join Date
    08-25-2017
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    19

    Re: VBA Picture insert from directoy in merged cell and scaling

    Thanks PCI! This works great!
    Last edited by heinzpol; 02-14-2020 at 09:31 AM.

  12. #12
    Registered User
    Join Date
    08-25-2017
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    19

    Re: VBA Picture insert from directoy in merged cell and scaling

    1. Is it possible to add to the code that a message pops up when there is no corresponding picture in the folder.
    2. Is it possible to delete the pictures in MyPicture when the macro starts. This will result in an empty MyPicture. Now the placed picture are not deleted when I13 is empty after an update

  13. #13
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Picture insert from directoy in merged cell and scaling

    See next code
    
    Option Explicit
    
    Sub Treat()
    Dim WkPath As String
    WkPath = ActiveWorkbook.Path & "\" & "Pictures"
     
    Dim PicRg  As Range
        Set PicRg = Range("MyPicture")
    Dim WkPicN As String
        WkPicN = Range("I13")
    
    Dim WkPic As Picture
    Dim H, W
    
    Dim WkRg As Range
    
        With ActiveSheet
            For Each WkPic In .Pictures
                Set WkRg = .Range(WkPic.TopLeftCell.Address & ":" & WkPic.BottomRightCell.Address)
                If Not Intersect(WkRg, PicRg) Is Nothing Then WkPic.Delete
            Next
        End With
        If (Len(WkPicN) = 0) Then Exit Sub
        
        Application.ScreenUpdating = False
        Set WkPic = ActiveSheet.Pictures.Insert(WkPath & "\" & WkPicN & ".jpg")
    '   Resize picture
        With WkPic
            .ShapeRange.LockAspectRatio = msoTrue
             W = PicRg.Cells(1, 1).Width + PicRg.Cells(1, 2).Width + PicRg.Cells(1, 3).Width
             H = PicRg.Cells(1, 1).Height + PicRg.Cells(2, 1).Height + PicRg.Cells(3, 1).Height
            .Height = 0.95 * H
            .Left = PicRg.Left + (W - .Width) / 2
            .Top = PicRg.Top + (H - .Height) / 2
        End With
        Application.ScreenUpdating = True
    
    End Sub

  14. #14
    Registered User
    Join Date
    08-25-2017
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    19

    Re: VBA Picture insert from directoy in merged cell and scaling

    Thnx again. Unfortunately the scaling part is nog working well with different type of photo’s. Sometimes to big, other times to small. Seems like for some pictures the width is maximised to the height of MyPictures frame.

  15. #15
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Picture insert from directoy in merged cell and scaling

    Can you send a phto where there is issue ?

  16. #16
    Registered User
    Join Date
    08-25-2017
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    19

    Re: VBA Picture insert from directoy in merged cell and scaling

    Here two examples in my workbook with pictures original:

    Attachment 662802
    Attachment 662803

  17. #17
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Picture insert from directoy in merged cell and scaling

    Links for attachments does not work
    You could try also
    Option Explicit
    
    Sub Treat()
    Dim WkPath As String
    WkPath = ActiveWorkbook.Path & "\" & "Pictures"
     
    Dim PicRg  As Range
        Set PicRg = Range("MyPicture")
    Dim WkPicN As String
        WkPicN = Range("I13")
    
    Dim WkPic As Picture
    Dim H, W
    
    Dim WkRg As Range
    
        With ActiveSheet
            For Each WkPic In .Pictures
                Set WkRg = .Range(WkPic.TopLeftCell.Address & ":" & WkPic.BottomRightCell.Address)
                If Not Intersect(WkRg, PicRg) Is Nothing Then WkPic.Delete
            Next
        End With
        If (Len(WkPicN) = 0) Then Exit Sub
        
        Application.ScreenUpdating = False
        Set WkPic = ActiveSheet.Pictures.Insert(WkPath & "\" & WkPicN & ".jpg")
    '   Resize picture
        With WkPic
            .ShapeRange.LockAspectRatio = msoTrue
             H = PicRg.Cells(1, 1).Height + PicRg.Cells(2, 1).Height + PicRg.Cells(3, 1).Height
            .Height = 0.95 * H
            .Top = PicRg.Top + (H - .Height) / 2
             W = PicRg.Cells(1, 1).Width + PicRg.Cells(1, 2).Width + PicRg.Cells(1, 3).Width
            .Left = PicRg.Left + (W - .Width) / 2
        End With
        Application.ScreenUpdating = True
    End Sub
    Last edited by PCI; 02-14-2020 at 02:38 PM.

  18. #18
    Registered User
    Join Date
    08-25-2017
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    19

    Re: VBA Picture insert from directoy in merged cell and scaling

    OK I figured out what it was: the pcitures are 5 or 6 mb and that is far too large. So I resized them and then it go's well with the original code!

    Only thing that lasts is: when thre is no corresponding picture in the pictures folder that the code gives an error. Solution to that:

    Can you add a line that says that the pictures is not found in the folder and that stops the macro.

    This is the latest code:


    Option Explicit
    
    Sub Treat()
    Dim WkPath As String
    WkPath = ActiveWorkbook.Path & "\" & "Pictures"
     
    Dim PicRg  As Range
        Set PicRg = Range("MyPicture")
    Dim WkPicN As String
        WkPicN = Range("I13")
        If (Len(WkPicN) = 0) Then Exit Sub
    
    Dim WkPic As Picture
    Dim H, W
    
    Dim WkRg As Range
        Application.ScreenUpdating = False
        With ActiveSheet
            For Each WkPic In .Pictures
                Set WkRg = .Range(WkPic.TopLeftCell.Address & ":" & WkPic.BottomRightCell.Address)
                If Not Intersect(WkRg, PicRg) Is Nothing Then WkPic.Delete
            Next
        End With
    
        
        Set WkPic = ActiveSheet.Pictures.Insert(WkPath & "\" & WkPicN & ".jpg")
    '   Resize picture
        With WkPic
            .ShapeRange.LockAspectRatio = msoTrue
             W = PicRg.Cells(1, 1).Width + PicRg.Cells(1, 2).Width + PicRg.Cells(1, 3).Width
             H = PicRg.Cells(1, 1).Height + PicRg.Cells(2, 1).Height + PicRg.Cells(3, 1).Height
            .Height = 0.95 * H
            .Left = PicRg.Left + (W - .Width) / 2
            .Top = PicRg.Top + (H - .Height) / 2
            
     '       .Width = (PicRg.Cells(1, 1).Width + PicRg.Cells(1, 2).Width + PicRg.Cells(1, 3).Width)
     '       .Height = (PicRg.Cells(1, 1).Height + PicRg.Cells(2, 1).Height + PicRg.Cells(3, 1).Height)
        End With
        Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files
    Last edited by heinzpol; 02-15-2020 at 05:41 AM.

  19. #19
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Picture insert from directoy in merged cell and scaling

    See next code
    
    Option Explicit
    
    Sub Treat()
    Dim WkPath As String
    WkPath = ActiveWorkbook.Path & "\" & "Pictures"
     
    Dim PicRg  As Range
        Set PicRg = Range("MyPicture")
    Dim WkPicN As String
        WkPicN = Range("I13")
        If (Len(WkPicN) = 0) Then Exit Sub
    
    Dim WkPic As Picture
    Dim H, W
    
    Dim WkRg As Range
        Application.ScreenUpdating = False
        With ActiveSheet
            For Each WkPic In .Pictures
                Set WkRg = .Range(WkPic.TopLeftCell.Address & ":" & WkPic.BottomRightCell.Address)
                If Not Intersect(WkRg, PicRg) Is Nothing Then WkPic.Delete
            Next
        End With
    
        On Error GoTo EndSub
        Set WkPic = ActiveSheet.Pictures.Insert(WkPath & "\" & WkPicN & ".jpg")
        On Error GoTo 0
        
    '   Resize picture
        With WkPic
            .ShapeRange.LockAspectRatio = msoTrue
             W = PicRg.Cells(1, 1).Width + PicRg.Cells(1, 2).Width + PicRg.Cells(1, 3).Width
             H = PicRg.Cells(1, 1).Height + PicRg.Cells(2, 1).Height + PicRg.Cells(3, 1).Height
            .Height = 0.95 * H
            .Left = PicRg.Left + (W - .Width) / 2
            .Top = PicRg.Top + (H - .Height) / 2
        End With
        Application.ScreenUpdating = True
    EndSub:
    End Sub

  20. #20
    Registered User
    Join Date
    08-25-2017
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    19

    Re: VBA Picture insert from directoy in merged cell and scaling

    Great! Many Thanks All working fine now!!

    Added also a message box in fault:

    Here's the code for the Module:

    Option Explicit
    
    Sub Treat()
    Dim WkPath As String
    WkPath = ActiveWorkbook.Path & "\" & "Pictures"
     
    Dim PicRg  As Range
        Set PicRg = Range("MyPicture")
    Dim WkPicN As String
        WkPicN = Range("J13")
        If (Len(WkPicN) = 0) Then Exit Sub
    
    Dim WkPic As Picture
    Dim H, W
    
    Dim WkRg As Range
        Application.ScreenUpdating = False
        With ActiveSheet
            For Each WkPic In .Pictures
                Set WkRg = .Range(WkPic.TopLeftCell.Address & ":" & WkPic.BottomRightCell.Address)
                If Not Intersect(WkRg, PicRg) Is Nothing Then WkPic.Delete
            Next
        End With
    
        On Error GoTo Errorhandler
        Set WkPic = ActiveSheet.Pictures.Insert(WkPath & "\" & WkPicN & ".jpg")
            
    '   Resize picture
        With WkPic
            .ShapeRange.LockAspectRatio = msoTrue
             W = PicRg.Cells(1, 1).Width + PicRg.Cells(1, 2).Width + PicRg.Cells(1, 3).Width
             H = PicRg.Cells(1, 1).Height + PicRg.Cells(2, 1).Height + PicRg.Cells(3, 1).Height
            .Height = 0.95 * H
            .Left = PicRg.Left + (W - .Width) / 2
            .Top = PicRg.Top + (H - .Height) / 2
        End With
        Application.ScreenUpdating = True
    
    Exit Sub
    Errorhandler:
        MsgBox "There is no corresponding picture in the 'Pictures' folder!"
    Exit Sub
    
    EndSub:
    End Sub
    And here's the code for the sheet with the picture:

    Private Sub Worksheet_Activate()
        Call Treat
    End Sub
    Last edited by heinzpol; 02-15-2020 at 09:30 AM.

  21. #21
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Picture insert from directoy in merged cell and scaling

    Yes, you customise the code, super.

  22. #22
    Registered User
    Join Date
    10-20-2022
    Location
    Germany
    MS-Off Ver
    MS Office 365
    Posts
    2

    Re: VBA Picture insert from directoy in merged cell and scaling

    Dear all! Thanks for your effort. I would like to follow up this topic and need your support. I would need some slightly changes in the coding. I use an excel sheet where I want to insert and embed a Picture in a fixed area of merged cells with a fixed hight. My current code only insert a link to the picture that will not work when the source folder of the pictures changes. The picture size only need a fixed hight. The width according to aspect ratio. The default directory can be a general one and does not need to be fixed in the code. But it would be great if it opens the last used file path when I already used it in the current session.

    Kindly ask for your support again! Thanks!

  23. #23
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: VBA Picture insert from directoy in merged cell and scaling

    Hello Zappatustra,
    As your need is different to the previous one in the thread it will be more effective to open your own thread and make reference to this one.
    Always keep in mind to attach an Excel file to be able to fit completely to yours specifs.
    PCI

+ 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] Hyperlink cell values to pdf file in a directoy
    By mubashir aziz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-03-2017, 07:52 AM
  2. Resize picture to fit merged cell
    By TitansGo in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-29-2016, 04:20 PM
  3. [SOLVED] Insert Picture in cell, click commanbutton to rename picture, lock cell and remove button
    By thecdnmole in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-28-2014, 10:05 PM
  4. [SOLVED] import picture to fit in merged cell
    By alexshaw76 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-01-2014, 09:35 PM
  5. Import picture to fit in merged cell
    By alexshaw76 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2014, 03:37 PM
  6. VBA for Picture insert at particular worksheet range. Picture name derived from cell.
    By Douglas2013 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-09-2013, 01:16 AM
  7. [SOLVED] How do i insert a picture in merged cels, with autom. resizing ?
    By Sven Ghyselinck in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-27-2006, 04:30 PM

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