|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub AutoFitPic()
Dim picW As Single, picH As Single
Dim cellW As Single, cellH As Single
Dim rtoW As Single, rtoH As Single
cellW = ActiveCell.Width
cellH = ActiveCell.Height
picW = Selection.ShapeRange.Width
picH = Selection.ShapeRange.Height
'重设图片的宽和高
rtoW = cellW / picW * 0.95
rtoH = cellH / picH * 0.95
If rtoW < rtoH Then
'Selection.ShapeRange.ScaleWidth rtoW, msoFalse, msoScaleFromTopLeft '适应列宽
Else
'Selection.ShapeRange.ScaleHeight rtoH, msoFalse, msoScaleFromTopLeft '适应行高
End If
Selection.ShapeRange.ScaleHeight rtoH, msoFalse, msoScaleFromTopLeft
picW = Selection.ShapeRange.Width
picH = Selection.ShapeRange.Height
'锁定图片锁定纵横比
'Selection.ShapeRange.LockAspectRatio = msoTrue
'图片的位置与大小随单元格变化而变化
'Selection.Placement = xlMoveAndSize
'Selection.ShapeRange.IncrementLeft (cellW - picW) / 2
Selection.ShapeRange.IncrementLeft 10
Selection.ShapeRange.IncrementTop (cellH - picH) / 2
End Sub
|
|