假设您知道图像的名称及其所在的单元格,则下面的代码会将图像居中放置在一个单元格中。熟悉动态循环的人是否会在一张工作表中搜索该工作表中的每个图像,将每个图像放在其当前单元格的中心?如果不可能,是否有办法更改代码,以便您可以遍历特定范围?例如A4:A10
Sub CenterImages()
With ActiveSheet.Shapes("Picture 1")
.Top = Range("B1").Top + (Range("B1").Height - .Height) / 2
.Left = Range("B1").Left + (Range("B1").Width - .Width) / 2
End With
End Sub
也许使用Shape.TopLeftCell
,如下所示:
Sub CenterImages()
Dim shp As Shape
For Each shp In ActiveSheet.Shapes
If shp.Type = msoPicture Then
With shp
.Top = .TopLeftCell.Top + (.TopLeftCell.Height - .Height) / 2
.Left = .TopLeftCell.Left + (.TopLeftCell.Width - .Width) / 2
End With
End If
Next
End Sub
如果要将其限制为一定范围内的图像,则可以更改为
If shp.Type = msoPicture _
And Not Intersect(shp.TopLeftCell, ActiveSheet.Range("A4:A10")) Is Nothing Then
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句