首页 > 解决方案 > 如何区分VBA代码excel中选择的图片?

问题描述

我是 VBA 编码的初学者。我尝试在Excel中排列3张图片如下:

  1. 使所有选定的图片大小相同。

  2. 将选择的 3 张图片彼此相距 188 点,排成 1 行。

我的问题是我不知道如何区分图片。使用我的代码,图片重叠。那是我的代码:

Sub ArrangePics()

' ArrangePics Macro

    Dim objPic As Object

    For Each objPic In ActiveSheet.Pictures
        With objPic.ShapeRange
            .LockAspectRatio = False
            .Height = Application.CentimetersToPoints(4.1)
            .Width = Application.CentimetersToPoints(5.1)
        End With
    Next

    Selection.ShapeRange.Distribute msoDistributeHorizontally, msoFalse


Dim intX As Integer
intX = 1
Dim i As Long

For i = 0 To 2 Step 1

   Selection.ShapeRange.Left = intX * 188

Next


'       Selection.Cut

End Sub 

标签: excelvba

解决方案


你可以在这之后:

Dim xPic As Long

Dim objPic As Object
For Each objPic In Selection
    If TypeOf objPic Is Picture Then ' be sure to deal with pics only
        With objPic.ShapeRange
            .LockAspectRatio = False
            .Height = Application.CentimetersToPoints(4.1)
            .Width = Application.CentimetersToPoints(5.1)

            .Top = 200 ' same row for all pictures
            .Left = xPic
            xPic = xPic + 188 ' update xPic
        End With
    End If
Next

推荐阅读