0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Excel】選択している画像を縦に連結してグループ化する

Posted at
Public Sub 選択している画像を縦に連結してグループ化する()

    If VarType(ActiveWindow.Selection) = vbObject Then
        Set tgtShapes = ActiveWindow.Selection.ShapeRange
        If ActiveWindow.Selection.ShapeRange.Count < 2 Then
            MsgBox "画像が2個以上選択されていません。" 
            End
        End If
        ' 左詰めで整列
        Selection.ShapeRange.Align msoAlignLefts, msoFalse
        ' 一番上の画像の位置を取得
        Dim preTop As Long: preTop = tgtShapes(1).Top
        For Each shp In tgtShapes
            If preTop > shp.Top Then
                preTop = shp.Top
            End If
        Next shp
        ' 連結処理
        Dim Count As Integer
        Dim tmpTop As Long: tmpTop = 0
        Count = 0
        For Each shp In tgtShapes
            tmpTop = preTop
            For Each compShp In tgtShapes
                '処理対象より上にある場合
                If shp.Top > compShp.Top Then
                    '上の画像の下に対照画像の高さを足す
                    tmpTop = tmpTop + compShp.Height
                End If
            Next compShp
            '処理対象の位置を更新
            If shp.Top > tmpTop Then
                shp.Top = tmpTop
            End If
        Next shp
        ' グループ化
        tgtShapes.Group
    Else
        MsgBox "画像が選択されていません。", vbCritical
    End If

End Sub

使用例

対象の画像を選択後、当マクロを実行する。

実行前

実行前.PNG

実行後

実行後.PNG

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?