excelvbaimageexcel-formula

Excel linked picture resizing


I have linked data in range A2:C5 to a image using "copy as picture". Then linked this picture with formula to the data range. Whenever there is change in the range data, the picture gets updated. This I use this picture like a watchwindow in another worksheet. However the image size is fixed for the range. If the range has only 2 rows of data, then image looks stretched & if there are more rows, then they become compressed. How to keep the picture size in proportion of range size.


Solution

  • It seems to me like a bug, or at least an inconsistent behavior. I did some tests, and oddly, when copying as "Picture", the size didn't adapt correctly when changing the address, but when copying as "Bitmap", it did.

    Was about to write that as answer but played around first (with both variations), and had both cases: A former correctly formatted image was stretched after changing the range, but also a former stretched image was sized correctly. I was not able to figure out a rule for that (if there is any).

    Best bet is to open the "format picture" dialog, go to the size tab and press the reset button.

    enter image description here

    An alternative is to use a little bit of code. The following can give you an idea. Assign it to the "OnAction"-property of the image. Now every time you click on the image, it will resize itself.

    Sub resizeSelectedImg()
        ' Who called me?
        On Error Resume Next
        Dim sh As Shape
        Set sh = ActiveSheet.Shapes(Application.Caller)
        On Error GoTo 0
        If sh Is Nothing Then Exit Sub
        
        ' Formula of the shape 
        Dim f As String
        On Error Resume Next
        f = sh.DrawingObject.Formula
        On Error GoTo 0
        If f = "" Then Exit Sub
        
        ' Get range
        On Error Resume Next
        Dim r As Range
        Set r = Range(f)
        On Error GoTo 0
        If r Is Nothing Then Exit Sub
        
        ' Resize shape
        sh.LockAspectRatio = False
        sh.Height = r.Height
        sh.Width = r.Width
        sh.LockAspectRatio = True
    End Sub