Excel VBA 図形操作入門|一瞬で配置・一括削除

コードはこちら

ショートカットキーに設定するのは、短いコード部分です。
(Sub ○() とかになっている箇所)

長い部分は基礎設定をしていて、直接呼び出すものではありません。

コードはこちら
Sub 図形を作る_セル基準(図形種類 As MsoAutoShapeType)
    
    Dim shp As Shape
    Dim c As Range
    
    Set c = Selection.Cells(1, 1)
    
    Set shp = ActiveSheet.Shapes.AddShape( _
        図形種類, _
        c.Left, _
        c.Top, _
        c.Width, _
        c.Height _
    )
    
    ' 塗りつぶしな
    shp.Fill.Visible = msoFalse
    
    ' 線の設
    With shp.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 0, 0)
        .Transparency = 0
        .Weight = 1.5
    End With

End Sub

Sub ○()
End Sub

Sub □()
    図形を作る_セル基準 msoShapeRectangle
End Sub

Sub △()
    図形を作る_セル基準 msoShapeIsoscelesTriangle
End Sub

Sub →()
    図形を作る_セル基準 msoShapeRightArrow
End Sub

Sub ()
    図形を作る_セル基準 msoShapeCloud
End Sub


Private Sub CreateStraightConnector( _
    ByVal beginArrow As MsoArrowheadStyle, _
    ByVal endArrow As MsoArrowheadStyle _
)
    Dim c1 As Range, c2 As Range
    Dim con As Shape
    
    Dim x1 As Double, y1 As Double, x2 As Double, y2 As Double

    ' Selection が Range でないケース対
    If TypeName(Selection) <> "Range" Then Exit Sub
    If Selection.Cells.Count < 1 Then Exit Sub

    If Selection.Cells.Count >= 2 Then
        ' 2セル:中心→中
        Set c1 = Selection.Cells(1)
        Set c2 = Selection.Cells(2)

        x1 = c1.Left + c1.Width / 2
        y1 = c1.Top + c1.Height / 2
        x2 = c2.Left + c2.Width / 2
        y2 = c2.Top + c2.Height / 2

    Else
        ' 1セル:左端→右端(高さは中央
        Set c1 = Selection.Cells(1)

        x1 = c1.Left
        y1 = c1.Top + c1.Height / 2
        x2 = c1.Left + c1.Width
        y2 = c1.Top + c1.Height / 2
    End If

    Set con = ActiveSheet.Shapes.AddConnector( _
        msoConnectorStraight, _
        x1, y1, x2, y2 _
    )

    With con.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 0, 0)
        .Weight = 1.5
        .BeginArrowheadStyle = beginArrow
        .EndArrowheadStyle = endArrow
    End With
End Sub

' 片方向矢印(→
Public Sub Run_Arrow()
    CreateStraightConnector msoArrowheadNone, msoArrowheadTriangle
End Sub

' 矢印なし(―
Public Sub Run_Line()
    CreateStraightConnector msoArrowheadNone, msoArrowheadNone
End Sub

' 両方向矢印(⇔
Public Sub Run_BiArrow()
    CreateStraightConnector msoArrowheadTriangle, msoArrowheadTriangle
End Sub

Sub 選択範囲にかかるオブジェクトを削除_安定版()

    Dim shp As Shape
    Dim rng As Range
    Dim i As Long
    
    ' 1. 選択されているのがセル(Range)であることを確
    If TypeName(Selection) <> "Range" Then
        MsgBox "セルを選択してから実行してください。", vbExclamation
        Exit Sub
    End If
    
    Set rng = Selection

    ' 2. Shapesを後ろからループ(削除時のインデックスずれ防止
    ' ※ActiveSheet.Shapes.Count から 1 まで逆順に回
    For i = ActiveSheet.Shapes.Count To 1 Step -1
        Set shp = ActiveSheet.Shapes(i)
        
        ' 判定ロジック(元のコードの考え方を採用
        ' 重なり判定:(図形の右 > 範囲の左) かつ (図形の左 < 範囲の右) ...
        If shp.Left + shp.Width > rng.Left And _
           shp.Left < rng.Left + rng.Width And _
           shp.Top + shp.Height > rng.Top And _
           shp.Top < rng.Top + rng.Height Then
            
            shp.Delete
            
        End If
    Next i

End Sub

ショートカットキーの管理

この記事を参考にしていただければ。

コードの設定の仕方

基礎的な内容はこちら。