はじめに
Excel の VBAマクロ と Visual Studio Code(VSCode) を連携する自作のVSCode拡張機能(excel-vba-sync1)に mermaidフローチャート自動作成機能を追加したのでその備忘録。
インストールやエクスポート/インポート方法などは以下の VSCode 拡張機能の記事に記述しているので、ここではモジュールからのフローチャート自動作成機能の使用方法のみ記述
今回追加した機能の概要 / 特長
- excel-vba-sync にて出力されたVBA モジュールから フローチャート作成用 JSONファイルを作成
- 作成したフローチャート作成用 JSONファイルから sub/function単位に mermaid フローチャートファイル(*.mmd) を作成
- vibecodingにて実装(githubcopilot + claude4による)
- フローチャート出力は簡易な検証のみ実施 (複雑な処理、ループ、性能等は確認していない)
対応環境:
- Windows 10/11(24H2)
- Microsoft Excel
- Visual Studio Code
基本的な使い方
1. フローチャート出力
- excel-vba-sync のエクスポートファイル(
.bas/.cls/.frm) に対して右クリック後 Generate VBA Flow Chart を選択
- mmdフォルダ が作成され、mmdファイルが作成される
UTF8出力となるため、文字化けしている場合はVSCodeの文字コードをUTF8に変更
- mmdファイルは Mermaid Preview2 等の VSCode 拡張機能で出力の確認が可能
参考
検証に使用した VBAコードと出力されたMermaidファイル
VBAコード
option Explicit
Sub OutputPrimes_2to10000()
Dim maxN As Long: maxN = 10000
Dim isComposite() As Boolean
Dim p As Long, multiple As Long
Dim n As Long, r As Long
Application.ScreenUpdating = False
' 合成数フラグ(False=素数候補, True=合成数)
ReDim isComposite(2 To maxN)
For p = 2 To Int(Sqr(maxN))
If Not isComposite(p) Then
multiple = p * p
Do While multiple <= maxN
isComposite(multiple) = True
multiple = multiple + p
Loop
End If
Next p
With ActiveSheet
.Range("A:A").ClearContents
r = 1
For n = 2 To maxN
If Not isComposite(n) Then
.Cells(r, 1).Value = n
r = r + 1
End If
Next n
End With
Application.ScreenUpdating = True
End Sub
Mermaid
flowchart TD
Module1_OutputPrimes_2to10000_n4(["Start"]);
Module1_OutputPrimes_2to10000_n4_2["Dim maxN As Long: maxN = 10000"];
Module1_OutputPrimes_2to10000_n5["Dim isComposite() As Boolean"];
Module1_OutputPrimes_2to10000_n6["Dim p As Long, multiple As Long"];
Module1_OutputPrimes_2to10000_n7["Dim n As Long, r As Long"];
Module1_OutputPrimes_2to10000_n9["Application.ScreenUpdating = False"];
Module1_OutputPrimes_2to10000_n12[["ReDim isComposite(2 To maxN)"]];
Module1_OutputPrimes_2to10000_n15[/"For p = 2 To Int(Sqr(maxN))"\];
Module1_OutputPrimes_2to10000_n16{"If Not isComposite(p)?"};
Module1_OutputPrimes_2to10000_n17["multiple = p * p"];
Module1_OutputPrimes_2to10000_n18[/"Do While multiple <= maxN"\];
Module1_OutputPrimes_2to10000_n19[["isComposite(multiple) = True"]];
Module1_OutputPrimes_2to10000_n20["multiple = multiple + p"];
Module1_OutputPrimes_2to10000_n21(( ));
Module1_OutputPrimes_2to10000_n22(( ));
Module1_OutputPrimes_2to10000_n23[\"For Next End"/];
Module1_OutputPrimes_2to10000_n26["With ActiveSheet"];
Module1_OutputPrimes_2to10000_n27[".Range("A:A").ClearContents"];
Module1_OutputPrimes_2to10000_n28["r = 1"];
Module1_OutputPrimes_2to10000_n29[/"For n = 2 To maxN"\];
Module1_OutputPrimes_2to10000_n30{"If Not isComposite(n)?"};
Module1_OutputPrimes_2to10000_n31[".Cells(r, 1).Value = n"];
Module1_OutputPrimes_2to10000_n32["r = r + 1"];
Module1_OutputPrimes_2to10000_n33(( ));
Module1_OutputPrimes_2to10000_n34[\"For Next End"/];
Module1_OutputPrimes_2to10000_n35(( ));
Module1_OutputPrimes_2to10000_n37["Application.ScreenUpdating = True"];
Module1_OutputPrimes_2to10000_n38(["End"]);
Module1_OutputPrimes_2to10000_n4 --> Module1_OutputPrimes_2to10000_n4_2;
Module1_OutputPrimes_2to10000_n4_2 --> Module1_OutputPrimes_2to10000_n5;
Module1_OutputPrimes_2to10000_n5 --> Module1_OutputPrimes_2to10000_n6;
Module1_OutputPrimes_2to10000_n6 --> Module1_OutputPrimes_2to10000_n7;
Module1_OutputPrimes_2to10000_n7 --> Module1_OutputPrimes_2to10000_n9;
Module1_OutputPrimes_2to10000_n9 --> Module1_OutputPrimes_2to10000_n12;
Module1_OutputPrimes_2to10000_n12 --> Module1_OutputPrimes_2to10000_n15;
Module1_OutputPrimes_2to10000_n15 --> Module1_OutputPrimes_2to10000_n16;
Module1_OutputPrimes_2to10000_n16 -->|No| Module1_OutputPrimes_2to10000_n17;
Module1_OutputPrimes_2to10000_n17 --> Module1_OutputPrimes_2to10000_n18;
Module1_OutputPrimes_2to10000_n18 --> Module1_OutputPrimes_2to10000_n19;
Module1_OutputPrimes_2to10000_n19 --> Module1_OutputPrimes_2to10000_n20;
Module1_OutputPrimes_2to10000_n20 --> Module1_OutputPrimes_2to10000_n21;
Module1_OutputPrimes_2to10000_n21 --> Module1_OutputPrimes_2to10000_n22;
Module1_OutputPrimes_2to10000_n16 -->|Yes| Module1_OutputPrimes_2to10000_n22;
Module1_OutputPrimes_2to10000_n22 --> Module1_OutputPrimes_2to10000_n23;
Module1_OutputPrimes_2to10000_n23 --> Module1_OutputPrimes_2to10000_n26;
Module1_OutputPrimes_2to10000_n26 --> Module1_OutputPrimes_2to10000_n27;
Module1_OutputPrimes_2to10000_n27 --> Module1_OutputPrimes_2to10000_n28;
Module1_OutputPrimes_2to10000_n28 --> Module1_OutputPrimes_2to10000_n29;
Module1_OutputPrimes_2to10000_n29 --> Module1_OutputPrimes_2to10000_n30;
Module1_OutputPrimes_2to10000_n30 -->|No| Module1_OutputPrimes_2to10000_n31;
Module1_OutputPrimes_2to10000_n31 --> Module1_OutputPrimes_2to10000_n32;
Module1_OutputPrimes_2to10000_n32 --> Module1_OutputPrimes_2to10000_n33;
Module1_OutputPrimes_2to10000_n30 -->|Yes| Module1_OutputPrimes_2to10000_n33;
Module1_OutputPrimes_2to10000_n33 --> Module1_OutputPrimes_2to10000_n34;
Module1_OutputPrimes_2to10000_n34 --> Module1_OutputPrimes_2to10000_n35;
Module1_OutputPrimes_2to10000_n35 --> Module1_OutputPrimes_2to10000_n37;
Module1_OutputPrimes_2to10000_n37 --> Module1_OutputPrimes_2to10000_n38;
Module1_OutputPrimes_2to10000_n21 -.-> Module1_OutputPrimes_2to10000_n18
Module1_OutputPrimes_2to10000_n34 -.-> Module1_OutputPrimes_2to10000_n29
Module1_OutputPrimes_2to10000_n23 -.-> Module1_OutputPrimes_2to10000_n15
- Qiita 上に 作成した mermaidを埋め込むと何故かレンダリングエラーになる
- Mermaid公式3 の Open Editorでは正常出力されるので、表記誤りではない(と思いたい)