Home > OS >  Load image to fit in merged cell
Load image to fit in merged cell

Time:05-14

I have a table that contains the file path, when the button is clicked the macro will display an image according to the url path. Here is my code (sourch : enter image description here

But I want the image to be in all merged cells. As shown in the following picture

enter image description here

Please let me know if you see anything that will fix this! I'm sure it's something simple, but I've been stuck for a while on this one.

CodePudding user response:

You can use the MergeArea property of the Range object to return the merged range. Your macro can amended as follows (untested) . . .

Sub Macro_1()
Dim cShape As Shape
Dim cRange As Range
Dim cColumn As Long
On Error Resume Next
Application.ScreenUpdating = False
Set xRange = ActiveSheet.Range("C5, G5, C8, G8")
For Each cell In xRange
    cName = cell
    ActiveSheet.Pictures.Insert(cName).Select
    Set cShape = Selection.ShapeRange.Item(1)
    If cShape Is Nothing Then GoTo line22
    cColumn = cell.Column
    Set cRange = cell.MergeArea
    With cShape
          .LockAspectRatio = msoFalse
            .Height = cRange.Height - 5
            .Width = cRange.Width - 5
            .Top = cRange.Top   2
            .Left = cRange.Left   2
            .Placement = xlMoveAndSize

    End With
line22:
        Set cShape = Nothing
    Next
    Application.ScreenUpdating = True
End Sub
  • Related