excel - VBA:如何将当前单元格中一个 Excel 工作表中的每个图像居中

标签 excel vba

假设您知道图像的名称和它所在的单元格,下面的代码将使图像在单元格中居中。是否有人熟悉动态循环,该循环将在单个工作表中搜索每个图像该工作表并将每个图像集中在当前单元格中?如果这是不可能的,是否有办法更改代码,以便可以循环遍历特定范围?例如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/

相关文章:

excel - 从 Excel 的 VBA 调用加载项函数

Excel 问题 "excel 4.0 function stored in defined names"

Excel SUMIF 列值匹配

validation - VBA Excel代码:cannot give named range to formula1 in validation

mysql - 在VBA中查询查询

excel - 一次选择高于限制值的所有单元格

excel - 用于获取列中每个唯一值的范围的宏

php - MySQL 导出到 Excel 写入整个 PHP 文件

excel - vba:查找文本中不存在的数字

excel - 如何从Excel调用VBA函数