[英]Scaling pictures using Excel VBA
I'm trying to scale pictures to fit on a cell of height 172.75. 我正在尝试缩放图片以适合高度为172.75的单元格。
If sPhoto > -1 Then
x.RowHeight = AltRow + x.Font.Size + 2
On Error GoTo IsError
factor = CSng(AltRow / Selection.ShapeRange.Height)
If factor > CSng(x.Width / Selection.ShapeRange.Width) Then
factor = CSng(x.Width / Selection.ShapeRange.Width)
End If
If factor < 0.5 Then
factor = factor / 3.8
End If
With Selection
.ShapeRange.LockAspectRatio = msoTrue
.ShapeRange.ScaleWidth factor, msoTrue, msoScaleFromTopLeft
.ShapeRange.ScaleHeight factor, msoTrue, msoScaleFromTopLeft
.ShapeRange.Top = x.Top
.ShapeRange.Left = x.Left
End With
End If
Some pictures have a really big size in terms of height and width in their original format. 有些图片的原始格式在高度和宽度方面都有很大的尺寸。 I need the scale factor to more flexible.
我需要比例因子以更灵活。
Found a way to scale the pictures to snap to the bounds of the cell. 找到了一种缩放图片以捕捉单元格边界的方法。
Dim AspectRatio As Double
Dim W, H As DoubleI
if SketchPhoto > -1 Then
x.RowHeight = AltRow + x.Font.Size + 2 'Adjusting height to fit the picture for each cell
With Selection.ShapeRange
.LockAspectRatio = msoTrue
AspectRatio = .Width / .Height
.Left = x.Left
.Top = x.Top
W = x.Width ' width of cell range
H = x.Height ' height of cell range
If (W / H < AspectRatio) Then
.Width = W - x.Font.Size + 0.5 ' scale picture to available width
Else
.Height = H - x.Font.Size + 0.5 ' scale picture to available height
End If
Range("A1").Activate
End With
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.