Sub ResizeImages() Dim shp As Shape Dim cell As Range

'set fixed height and width for images
Const imgHeight As Double = 100
Const imgWidth As Double = 100

'set cell addresses where images should be resized and repositioned
Dim cellAddresses As Variant
cellAddresses = Array('A1', 'B3', 'D2')

For Each shp In ActiveSheet.Shapes
    On Error Resume Next 'ignore errors
    
    'check if shape's top-left cell address matches any specified address
    Set cell = Range(shp.TopLeftCell.Address)
    If IsError(Application.Match(cell.Address, cellAddresses, 0)) Then
        'do nothing
    Else
        'resize and reposition image
        shp.LockAspectRatio = False 'allow image to be resized proportionally
        shp.Height = imgHeight
        shp.Width = imgWidth
        shp.Top = cell.Top + (cell.Height - shp.Height) / 2
        shp.Left = cell.Left + (cell.Width - shp.Width) / 2
    End If
    
    On Error GoTo 0 'reset error handling
Next shp

End Sub

VBA Macro: Resize and Reposition Images in Excel Based on Cell Location

原文地址: https://www.cveoy.top/t/topic/mAz3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录