Header Ads

VBA Excel - Chuyển tất cả các ảnh vào trung tâm cell và 'Move and size with cells'

VBA Excel - Chuyển tất cả các ảnh vào trung tâm cell và 'Move and size with cells'





Sub CenterImages()
    ' Declare a variable to hold each shape in the worksheet
    Dim shp As Shape

    ' Loop through all shapes in the active sheet
    For Each shp In ActiveSheet.Shapes
        ' Check if the shape is a picture
        If shp.Type = msoPicture Then
            With shp
                ' Set the placement mode to "Move and Size with Cells"
                .Placement = xlMoveAndSize
                ' Center the picture vertically in the cell
                .Top = .TopLeftCell.Top + (.TopLeftCell.Height - .Height) / 2
                ' Center the picture horizontally in the cell
                .Left = .TopLeftCell.Left + (.TopLeftCell.Width - .Width) / 2
            End With
        End If
    Next shp
End Sub

Để sử dụng đoạn mã này:
  1. Mở Excel.
  2. Nhấn Alt + F11 để mở trình soạn thảo VBA.
  3. Chọn Insert > Module để tạo một module mới.
  4. Sao chép và dán đoạn mã trên vào module vừa tạo.
  5. Đóng trình soạn thảo VBA.
  6. Quay lại Excel, nhấn Alt + F8, chọn CenterImages và nhấn Run.

Nếu bài viết này hữu ích, anh em có thể mời mình cốc sinh tố lúa mạch theo số tài khoản TPbank 3355 3618 888 (quét QR). Để tạo ra 1 bài viết, mình mất trung bình 2-3 tiếng. Một sự ủng hộ hơn ngàn lời khích lệ, để mình có thể duy trì trang web và tạo ra nhiều bài viết chất lượng hơn. Trân trọng cảm ơn tình cảm của anh em!

No comments

Powered by Blogger.