Change image and row height in response to changes in column width to maintain aspect ratio

Magic Polygon

New Member
Joined
Aug 20, 2023
Messages
30
Office Version
  1. 2019
Platform
  1. Windows
53d5b418641226fd4a7ce9c6a676c784.png

Suppose I have column A, like above, and I lengthen the column. It does this:
1693771867273.png

Instead of this:
1693772135305.png

In other words, I don't know how to force other dimensions (image and row height) to change to preserve the aspect ratio and fitted framing of the image made by its containing cell.

VBA Code:
'Determine the empty row
EmptyRow = WorksheetFunction.CountA(Range("B:B")) + 1

'Insert and format photo
Set photo = ActiveSheet.Pictures.Insert(ImagePath)
With photo

    .Left = ActiveSheet.Cells(EmptyRow, 1).Left
    .Top = ActiveSheet.Cells(EmptyRow, 1).Top
   
    With .ShapeRange
        .LockAspectRatio = msoTrue
        .Width = Cells(EmptyRow, 1).Width
    End With
   
    'Set the format of the picture cell
    ActiveSheet.Rows(EmptyRow).RowHeight = photo.ShapeRange.Height
   
    .Placement = 1
   
End With
 

Attachments

  • 1693771811191.png
    1693771811191.png
    168.8 KB · Views: 8

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You will need to calculate the initial ratio between the columnwidth and rowheight in pixels when first adding the picture and then multiply this ratio by the new columnwidth to get the correct new rowheight.
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Excel VBA: Transferring user-selected images from a userform to a worksheet with conserved aspect ratio and responsive fit
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top