0
votes

Currently, I'm using an Excel macro that inserts only one picture into a specific merged cell. The spreadsheet is designed such that it has multiple sections of 4 merged cells (in a 2x2 format) where the photos are positioned and re-sized into them. Each section has a button at the bottom of the section that prompts the macro and allows me insert one picture. But this gets tiresome since I can only do this with one picture, so I would like to expand the macro by being able to select all 4 photos, insert them, and position them to the specific cells.

The code is shown below:

Sub insertpictureinactivecell()
    
    Dim strFile As String
    Dim rng As Range
    Dim Sh As Shape
    
    Const cFile As String = "Image Files(*.jpg),"
         strFile = Application.GetOpenFilename(fileFilter:=cFile)
        If strFile = "False" Then
         Else
          Set rng = ActiveCell
          Set rng = rng.MergeArea
          With rng
           Set Sh = ActiveSheet.Shapes.AddPicture(Filename:=strFile, linktoFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
           Sh.LockAspectRatio = msoFalse
           
        End With
        Set Sh = Nothing
        Set rng = Nothing
        End If
         
    End Sub

I've tried Googling multiple solutions for this but I'm still new to Excel VBA so it was difficult for me understand them. Can anyone suggest how to solve this problem?

You'll need a loop and in each iteration change the top, left etc values accordingly.SJR
Thanks! Added a for loop, modified my spreadsheet a bit to make spacing between each cell more consistent, and now it works.pwalker