Adjusting row height

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Adjusting row height

Post by ABabeNChrist »

I’m using this code to insert a photo into selected cell, is it possible to resize row height prior to inserting photo. This way I can keep row height down to a minimum, until a photo is to be inserted

Code: Select all

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Range("A1,C1,E2"), Target) Is Nothing Then
        Cancel = True    ' Don't perform the standard action
        ' Your code here; Target is the cell being double-clicked
        Dim Pic As Excel.Picture
        Dim PicLocation As String
        Dim MyRange As Range

        PicLocation = Application.GetOpenFilename(FileFilter:="Pic Files (*.jpg;*.bmp), *.jpg;*.bmp", Title:="Browse to select a picture")

        If PicLocation = "False" Then Exit Sub

        Set Pic = Me.Pictures.Insert(PicLocation)

        With Pic.ShapeRange
            .Left = Target.Left
            .Top = Target.Top
            .LockAspectRatio = msoFalse
            .ZOrder msoBringForward
            If .Width > .Height Then
                .Width = Target.Width
                If .Height > Target.Height Then .Height = Target.Height
            Else
                .Height = Target.Height
                If .Width > Target.Width Then .Width = Target.Width
            End If
        End With

        With Pic
            .Placement = xlMoveAndSize
            .PrintObject = True
        End With

    End If
End Sub

User avatar
HansV
Administrator
Posts: 78537
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Adjusting row height

Post by HansV »

Your code resizes the picture to fit the cell. You could reverse that and resize the cell to fit the picture.
But if the user inserts a large picture, the result may not be desirable - the cell might become too wide and/or too tall.
So you'll have to think about what is acceptable...
Best wishes,
Hans