假设您知道图像的名称和它所在的单元格,下面的代码将使图像在单元格中居中。是否有人熟悉动态循环,该循环将在单个工作表中搜索每个图像该工作表并将每个图像集中在当前单元格中?如果这是不可能的,是否有办法更改代码,以便可以循环遍历特定范围?例如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
关于excel - VBA:如何将当前单元格中一个 Excel 工作表中的每个图像居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63708051/