i need help modifying an existing VBA to resize images

danielalvz

New Member
Joined
Jan 29, 2022
Messages
9
Office Version
  1. 2011
Platform
  1. Windows
i have an existing VBA code to rezise images, but does no resize to expecific cell width and height, could any one help with the missing pieces to be changes to fix a cell, code is below, thank you in advance
Option Explicit

Public Sub FITIMAGE()
Dim X As Single
Dim Y As Single
With Selection
X = .Width / .Height
End With
With Selection.TopLeftCell
Y = .Width / .RowHeight
End With
Select Case X / Y
Case Is > 1
With Selection
.Width = .TopLeftCell.Width
.Height = .Width / X
End With
Case Else
With Selection
.Height = .TopLeftCell.RowHeight
.Width = .Height * X
End With
End Select
With Selection
.Top = .TopLeftCell.Top
.Left = .TopLeftCell.Left
End With
Exit Sub

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Your macro is in working condition. Just select the image and launch the macro. As it stands it maintains the aspect ratio of the image.
 
Upvote 0
Your macro is in working condition. Just select the image and launch the macro. As it stands it maintains the aspect ratio of the image.
hey thanks for your replay, i dont really need the images to maintain the aspect ratio, I basically need it to fit on specific size cell
 
Upvote 0
If I correctly understood this is all you need to adapt your selected image to the cell. The upper left corner of the image has to be in the destination cell.
VBA Code:
Option Explicit
Public Sub FITIMAGE()
    With Selection
        'don't care of image aspect ratio
        .ShapeRange.LockAspectRatio = msoFalse
        'set size of image to cell size
        .Width = .TopLeftCell.Width
        .Height = .TopLeftCell.Height
        'align image to the upper left corner of cell
        .Top = .TopLeftCell.Top
        .Left = .TopLeftCell.Left
    End With
End Sub
 
Upvote 0
If I correctly understood this is all you need to adapt your selected image to the cell. The upper left corner of the image has to be in the destination cell.
VBA Code:
Option Explicit
Public Sub FITIMAGE()
    With Selection
        'don't care of image aspect ratio
        .ShapeRange.LockAspectRatio = msoFalse
        'set size of image to cell size
        .Width = .TopLeftCell.Width
        .Height = .TopLeftCell.Height
        'align image to the upper left corner of cell
        .Top = .TopLeftCell.Top
        .Left = .TopLeftCell.Left
    End With
End Sub
perfect just what i needed thanks
 
Upvote 0
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

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