1
0

Excelで処理フローを書くだって?

Posted at

Excelで処理フローを書いている現場って結構あるよね

なんとかして楽をしたい...

めちゃくちゃ簡単なフローなら、これで自動的に作成できるはず。

Sub CreateDynamicFlowchart()
    Dim ws As Worksheet
    Set ws = ActiveSheet

    ' ステップと図形の種類を格納するコレクション
    Dim stepsAndShapes As Collection
    Set stepsAndShapes = New Collection

    ' ステップと図形の種類を1行で追加
    stepsAndShapes.Add Array("データを読み込む", msoShapeRectangle)
    stepsAndShapes.Add Array("データをチェック", msoShapeDiamond)
    stepsAndShapes.Add Array("DBに登録", msoShapeRectangle)

    Dim i As Integer
    Dim item As Variant
    Dim shape As Shape
    Dim lastShape As Shape
    Dim topPosition As Single
    Dim leftPosition As Single

    ' 図形の初期位置
    topPosition = 50
    leftPosition = 100

    For Each item In stepsAndShapes
        ' 指定された種類の図形を作成
        Set shape = ws.Shapes.AddShape(item(1), leftPosition, topPosition, 150, 50)
        shape.TextFrame2.TextRange.Text = item(0)
        shape.Line.Weight = 2
        
        ' 矢印付き直線で前のシェイプと現在のシェイプをつなぐ
        If Not lastShape Is Nothing Then
            Dim connector As Shape
            Set connector = ws.Shapes.AddConnector(msoConnectorStraight, 0, 0, 100, 100)
            With connector
                .Line.EndArrowheadStyle = msoArrowheadTriangle
                .Line.ForeColor.RGB = RGB(0, 0, 0)
                .Line.Weight = 2
                .ConnectorFormat.BeginConnect lastShape, 1
                .ConnectorFormat.EndConnect shape, 1
                .RerouteConnections
            End With
        End If
        
        ' 次のシェイプの位置を更新
        topPosition = topPosition + 100
        Set lastShape = shape
    Next item
End Sub


ほか

分岐処理も簡単に書きたい....

1
0
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
0