Excel VBA | 自動生成ツール(リボンボタン対応版)

Excel VBA VBA
スポンサーリンク

自動生成ツール(リボンボタン対応版)

Excel VBA で UI 部品(カレンダー、検索フォーム、ログイン、一覧→詳細など)を自動生成する仕組みをまとめます。
目的は「ボタン1つで、指定したUIをワークブックへ自動配置・自動作成する」ことです。


全体構成

このツールは次の3層で構成します。

  1. UI 設定データ層(Config)
    • 生成するUIの種類
    • 必要なコントロール
    • レイアウト(配置位置、サイズ)
    • カラー / フォント規定
    • 入力規則
  2. UI 自動生成エンジン(Generator)
    • ユーザーフォームやシートUIを自動作成
    • テンプレのコードを自動注入
    • コントロールの配置(左・上・幅・高さ)
  3. リボンボタン(Ribbon XML)
    • “UI生成メニュー”を Excel リボンに追加
    • 押したボタンに応じて Generator が動作

リボンボタンの構成

リボンは customUI(Office 2010 以降リボンXML) を使用して実装します。

構成例

  • UI部品ライブラリ
    • カレンダー入力を生成
    • 日付範囲ピッカーを生成
    • プログレスバーを生成
    • 検索ダイアログを生成
    • ログイン画面を生成
    • 一覧→詳細UIを生成
    • 行編集フォームを生成
    • ドロップダウン一覧UIを生成
    • 高機能検索フォームを生成
    • すべてまとめて作成(プロジェクト雛形)

リボンXML(最小テンプレ)

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
  <ribbon>
    <tabs>
      <tab id="tabUI" label="UI部品ツール">
        <group id="grpGen" label="UI生成">
          <button id="btnCal" label="カレンダー入力" onAction="GenCalendar"/>
          <button id="btnRange" label="日付範囲ピッカー" onAction="GenRangePicker"/>
          <button id="btnProg" label="進捗バー" onAction="GenProgress"/>
          <button id="btnSearch" label="検索フォーム" onAction="GenSearch"/>
          <button id="btnLogin" label="ログイン画面" onAction="GenLogin"/>
          <button id="btnListDetail" label="一覧→詳細" onAction="GenListDetail"/>
          <button id="btnCRUD" label="行編集フォーム" onAction="GenCRUD"/>
          <button id="btnDDL" label="ドロップダウン一覧" onAction="GenDropdownList"/>
          <button id="btnAdv" label="高機能検索" onAction="GenAdvSearch"/>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>
VB

UI 自動生成エンジン(Generator)の基本構造

共通の自動生成関数を作っておくと、全 UI が “モジュール化” されて使いやすくなります。

共通モジュール:UI_Generator.bas

Option Explicit

'--- ユーザーフォームを生成して返す
Function CreateForm(ByVal formName As String, Optional width As Long = 400, Optional height As Long = 300) As VBComponent
    Dim vbComp As VBComponent
    Set vbComp = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
    vbComp.Name = formName
    
    With vbComp.Designer
        .Width = width
        .Height = height
    End With
    
    Set CreateForm = vbComp
End Function

'--- コントロールの共通追加
Function AddControl(frm As VBComponent, ctrlType As String, ctrlName As String, left As Long, top As Long, width As Long, height As Long) As MSForms.Control
    Dim c As MSForms.Control
    Set c = frm.Designer.Controls.Add(ctrlType, ctrlName)
    
    c.Left = left
    c.Top = top
    c.Width = width
    c.Height = height
    
    Set AddControl = c
End Function
VB

すべての UI 部品は、この共通関数を使って作られます。


カレンダー入力フォームの自動生成

Sub GenCalendar(control As IRibbonControl)

    Dim frm As VBComponent
    Set frm = CreateForm("frmCalendar", 300, 280)

    'ラベル
    AddControl frm, "Forms.Label.1", "lbl", 20, 20, 200, 18
    frm.Designer.Controls("lbl").Caption = "日付を選択してください"

    'カレンダーコントロール(MonthView または DTPicker)
    AddControl frm, "MSComCtl2.DTPicker.2", "dtp", 20, 60, 200, 20

    'OKボタン
    AddControl frm, "Forms.CommandButton.1", "btnOK", 160, 200, 80, 30
    frm.Designer.Controls("btnOK").Caption = "決定"

End Sub
VB

日付範囲ピッカー(From–To)

Sub GenRangePicker(control As IRibbonControl)
    Dim frm As VBComponent
    Set frm = CreateForm("frmRange", 340, 180)
    
    AddControl frm, "Forms.Label.1", "lbl1", 20, 20, 100, 18
    frm.Designer.Controls("lbl1").Caption = "開始日"
    
    AddControl frm, "MSComCtl2.DTPicker.2", "dtpStart", 120, 20, 150, 20

    AddControl frm, "Forms.Label.1", "lbl2", 20, 60, 100, 18
    frm.Designer.Controls("lbl2").Caption = "終了日"
    
    AddControl frm, "MSComCtl2.DTPicker.2", "dtpEnd", 120, 60, 150, 20

    AddControl frm, "Forms.CommandButton.1", "btnOK", 200, 110, 80, 30
    frm.Designer.Controls("btnOK").Caption = "決定"
End Sub
VB

プログレスバー(進捗バー)

Sub GenProgress(control As IRibbonControl)
    Dim frm As VBComponent
    Set frm = CreateForm("frmProgress", 300, 120)

    AddControl frm, "Forms.Label.1", "lblMsg", 20, 20, 260, 16
    frm.Designer.Controls("lblMsg").Caption = "処理中..."

    AddControl frm, "Forms.Frame.1", "barFrame", 20, 50, 260, 20
    AddControl frm, "Forms.Label.1", "barValue", 20, 50, 0, 20
    frm.Designer.Controls("barValue").BackColor = vbBlue
End Sub
VB

Excel版「検索ダイアログ」

  • キーワード入力
  • AND/OR設定(簡易)
  • 検索対象列の選択
  • 実行ボタン
Sub GenSearch(control As IRibbonControl)
    Dim frm As VBComponent
    Set frm = CreateForm("frmSearch", 350, 220)

    AddControl frm, "Forms.Label.1", "lblKey", 20, 20, 100, 18
    frm.Designer.Controls("lblKey").Caption = "キーワード"

    AddControl frm, "Forms.TextBox.1", "txtKey", 120, 20, 180, 20

    AddControl frm, "Forms.ComboBox.1", "cmbCol", 120, 60, 180, 20
    frm.Designer.Controls("cmbCol").AddItem "列A"
    frm.Designer.Controls("cmbCol").AddItem "列B"

    AddControl frm, "Forms.CommandButton.1", "btnSearch", 200, 140, 80, 30
    frm.Designer.Controls("btnSearch").Caption = "検索"
End Sub
VB

Excel版「ログイン画面」

Sub GenLogin(control As IRibbonControl)
    Dim frm As VBComponent
    Set frm = CreateForm("frmLogin", 300, 200)

    AddControl frm, "Forms.Label.1", "lblUser", 20, 30, 80, 18
    frm.Designer.Controls("lblUser").Caption = "ユーザー名"

    AddControl frm, "Forms.TextBox.1", "txtUser", 120, 30, 140, 20

    AddControl frm, "Forms.Label.1", "lblPass", 20, 70, 80, 18
    frm.Designer.Controls("lblPass").Caption = "パスワード"

    AddControl frm, "Forms.TextBox.1", "txtPass", 120, 70, 140, 20
    frm.Designer.Controls("txtPass").PasswordChar = "*"

    AddControl frm, "Forms.CommandButton.1", "btnLogin", 180, 120, 80, 30
    frm.Designer.Controls("btnLogin").Caption = "ログイン"
End Sub
VB

一覧 → 詳細画面 UI

  • 左側:一覧(ListBox)
  • 右側:詳細エリア
  • 選択すると項目が右に表示
Sub GenListDetail(control As IRibbonControl)
    Dim frm As VBComponent
    Set frm = CreateForm("frmListDetail", 600, 350)

    AddControl frm, "Forms.ListBox.1", "lst", 20, 20, 220, 300

    AddControl frm, "Forms.Frame.1", "frmDetail", 260, 20, 320, 300
    frm.Designer.Controls("frmDetail").Caption = "詳細"
End Sub
VB

行編集フォーム(Add / Edit / Delete)

Sub GenCRUD(control As IRibbonControl)
    Dim frm As VBComponent
    Set frm = CreateForm("frmCRUD", 400, 280)

    AddControl frm, "Forms.TextBox.1", "txtName", 120, 20, 200, 20
    AddControl frm, "Forms.TextBox.1", "txtValue", 120, 60, 200, 20

    AddControl frm, "Forms.CommandButton.1", "btnAdd", 40, 120, 80, 30
    frm.Designer.Controls("btnAdd").Caption = "追加"

    AddControl frm, "Forms.CommandButton.1", "btnEdit", 150, 120, 80, 30
    frm.Designer.Controls("btnEdit").Caption = "更新"

    AddControl frm, "Forms.CommandButton.1", "btnDelete", 260, 120, 80, 30
    frm.Designer.Controls("btnDelete").Caption = "削除"
End Sub
VB

ドロップダウンつき一覧 UI

Sub GenDropdownList(control As IRibbonControl)
    Dim frm As VBComponent
    Set frm = CreateForm("frmDDLList", 400, 300)

    AddControl frm, "Forms.ComboBox.1", "cmbFilter", 20, 20, 200, 20
    AddControl frm, "Forms.ListBox.1", "lstMain", 20, 60, 350, 200
End Sub
VB

高機能検索フォーム(AND/OR 条件)

Sub GenAdvSearch(control As IRibbonControl)
    Dim frm As VBComponent
    Set frm = CreateForm("frmAdvSearch", 420, 300)

    AddControl frm, "Forms.TextBox.1", "txtKey1", 20, 40, 150, 20
    AddControl frm, "Forms.TextBox.1", "txtKey2", 20, 80, 150, 20

    AddControl frm, "Forms.ComboBox.1", "cmbMode", 200, 40, 150, 20
    frm.Designer.Controls("cmbMode").AddItem "AND"
    frm.Designer.Controls("cmbMode").AddItem "OR"

    AddControl frm, "Forms.CommandButton.1", "btnExec", 260, 220, 80, 30
    frm.Designer.Controls("btnExec").Caption = "検索"
End Sub
VB
タイトルとURLをコピーしました