LoginSignup
1
2

More than 3 years have passed since last update.

図形の位置を調整するExcelマクロ

Posted at

これは

Excelでの資料作成時に意外と便利かもしれない、ちょっとしたマクロです。
複数の図形を選択した状態で当マクロを実行すると、
一つ目に選択したものを基準にして、図形の位置や大きさを調整します。

以前投稿したPowerPoint用のVBScript の焼き直しです。)

コード

Sub 図形位置調整(Optional  As Boolean = False, Optional  As Boolean = False, _
                Optional  As Boolean = False, Optional  As Boolean = False, _
                Optional  As Boolean = False, Optional  As Boolean = False, _
                Optional  As Boolean = False, Optional  As Boolean = False, _
                Optional  As Long = 5)

    Dim obj As Object, shpRng As ShapeRange
    Set obj = ActiveWindow.Selection
    If Not TypeName(obj) = "DrawingObjects" Then Exit Sub
    Set shpRng = obj.ShapeRange

    Dim i, l, t, w, h, r, b
    For i = 1 To shpRng.Count
        With shpRng.Item(i)
            If i = 1 Then
                l = .Left
                t = .Top
                w = .Width
                h = .Height
                r = l + w
                b = t + h
            Else
                If  Then .Left = l
                If  Then .Top = t
                If  Then .Width = w
                If  Then .Height = h
                If  Then .Width = r - .Left
                If  Then .Height = b - .Top
                If  Then .Left = r + 
                If  Then r = .Left + .Width
                If  Then .Top = b + 
                If  Then b = .Top + .Height
            End If
        End With
    Next
End Sub

使用例

よく使う作業をこんな感じでそれぞれ定義します。
クイックアクセスツールバーなどに登録しておくのもよいでしょう。

Sub 左端を揃える()
    図形位置調整 左:=True
End Sub
Sub 左端と幅を揃えて縦に整列()
    図形位置調整 左:=True, :=True, :=True, :=10
End Sub
1
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
1
2