エクセル研究室−Excelの基本的な使い方、応用技を紹介

図形をグループ化する


図形をグループ化する



図形を3種類作成し、全てグループ化します。
グループ化された図形の四角形だけを探し、幅を2倍にします。

Sub Sample100()
    Dim SN(0 To 2) As String, C As Shape, D As Shape
    '四角形を描画します
    With ActiveSheet.Shapes.AddShape(msoShapeRectangle, 50, 50, 100, 100)
        .Fill.ForeColor.RGB = RGB(255, 0, 0)
        .Line.ForeColor.RGB = RGB(255, 0, 0)
        SN(0) = .Name
    End With
    '円形を描画します
    With ActiveSheet.Shapes.AddShape(msoShapeOval, 20, 20, 70, 70)
        .Fill.ForeColor.RGB = RGB(0, 0, 255)
        .Line.ForeColor.RGB = RGB(0, 0, 255)
        SN(1) = .Name
    End With
    '月形を描画します
    With ActiveSheet.Shapes.AddShape(msoShapeMoon, 70, 70, 50, 50)
        .Fill.ForeColor.RGB = RGB(255, 255, 0)
        .Line.ForeColor.RGB = RGB(255, 255, 0)
        SN(2) = .Name
    End With
    '四角形、円形、月形を全てグループ化します
    ActiveSheet.Shapes.Range(SN).Group
    MsgBox "図形をグループ化しました"
    '1秒間待機
    Application.Wait Now + TimeValue("00:00:01")
    MsgBox "グループ化された図形の四角だけ幅を2倍に伸ばします"
    '全ての図形からグループ化された図形の四角形の幅を変更
    For Each C In ActiveSheet.Shapes
      For Each D In C.GroupItems
        If D.AutoShapeType = msoShapeRectangle Then
         D.Width = D.Width * 2
        End If
      Next D
    Next C
End Sub







エクセルVBAテクニック集トップ