Enum ImageModeEnum
FitCell_MaintainAspect = 1 'resizes the image to fit inside the cell, maintaining aspect ratio.
FitCell_IgnoreAspect = 2 'stretches or compresses the image to fit inside the cell, ignoring aspect ratio.
OriginalSize = 3 'leaves the image at original size, which may cause cropping.
CustomSize = 4 'allows the specification of a custom size.
End Enum
Function IMAGE(TargetURL As String, Optional ImageMode As ImageModeEnum = 3, Optional CustomHeight As Long = -1, Optional CustomWidth As Long = -1)
Dim TargetCell As Range, Img As Object
Dim CallerCell As Variant
CallerCell = Application.Caller.Address
If VarType(CallerCell) = vbString Then
Set TargetCell = Range(CallerCell)
Set Img = ActiveSheet.Pictures.Insert(TargetURL)
With Img
.Top = TargetCell.Top
.Left = TargetCell.Left
.ShapeRange.LockAspectRatio = IIf(ImageMode = 2, msoFalse, msoTrue)
.Placement = xlMoveAndSize
Select Case ImageMode
Case FitCell_MaintainAspect
If .Width > .Height Then
.Width = TargetCell.Width
Else
.Height = TargetCell.Height
End If
Case FitCell_IgnoreAspect
.Width = TargetCell.Width
.Height = TargetCell.Height
Case CustomSize
.ShapeRange.LockAspectRatio = msoFalse
If CustomHeight >= 0 And CustomWidth >= 0 Then
.Width = CustomWidth
.Height = CustomHeight
ElseIf CustomHeight >= 0 Then
.Height = CustomHeight
Else
.Width = CustomWidth
End If
End Select
End With
End If
IMAGE = ""
End Function