Excel VBA | 自動生成ツール(UI オートデザイナー)

Excel VBA VBA
スポンサーリンク

以下に、「ボタンを押すと UI が自動デザインされる Excel VBA 自動生成ツール」のテンプレートをまとめます。
このツールは、指定した UI 種類をクリックすると UserForm/ボタン/ラベルなどを自動で配置し、コードまで自動生成してくれる“プロの開発効率化ツール”です。


1. 全体構成

このツールは次で構成されます

  1. メイン画面:UI_Generator(UserForm)
    • 生成したい UI の種類を選択
    • 「生成」ボタンを押すとフォームが自動作成される
  2. UI 自動生成モジュール(Module: modUIBuilder)
    • UserForm を新規作成
    • コントロールを自動配置
    • コードを注入
    • 名前・位置・サイズを整形

2. メイン画面(UserForm: UI_Generator)

■ 配置するコントロール

種類名前用途
ListBoxlstUI自動生成したい UI の選択
CommandButtonbtnGen生成実行
Labellblタイトル

初期化コード

Private Sub UserForm_Initialize()
    lstUI.Clear
    lstUI.AddItem "カレンダー入力"
    lstUI.AddItem "日付範囲ピッカー"
    lstUI.AddItem "プログレスバー"
    lstUI.AddItem "検索ダイアログ"
    lstUI.AddItem "ログイン画面"
    lstUI.AddItem "一覧 → 詳細画面"
    lstUI.AddItem "行編集フォーム(Add/Edit/Delete)"
    lstUI.AddItem "ドロップダウン一覧 UI"
    lstUI.AddItem "高機能検索フォーム(AND/OR)"
End Sub
VB

実行ボタン

Private Sub btnGen_Click()
    If lstUI.ListIndex = -1 Then
        MsgBox "UI を選択してください"
        Exit Sub
    End If
    
    Call BuildUI(lstUI.Value)
End Sub
VB

3. 自動生成モジュール(modUIBuilder)

基本:新しい UserForm を作る機能

Function CreateNewForm(formName As String) As VBComponent
    Dim vbComp As VBComponent
    Set vbComp = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
    vbComp.Name = formName
    Set CreateNewForm = vbComp
End Function
VB

コントロール追加テンプレ

Sub AddControl(frm As VBComponent, ctrlType As String, ctrlName As String, _
               x As Single, y As Single, w As Single, h As Single, _
               Optional caption As String = "")
    
    Dim c As MSForms.Control
    Set c = frm.Designer.Controls.Add(ctrlType, ctrlName, True)
    
    c.Left = x
    c.Top = y
    c.Width = w
    c.Height = h
    
    If caption <> "" Then c.Caption = caption
End Sub
VB

4. UI種類ごとに自動生成処理を実装

4-1: カレンダー入力

Sub BuildUI_Calendar(frm As VBComponent)
    Dim i As Long, r As Long, c As Long
    For r = 0 To 5
        For c = 1 To 7
            AddControl frm, "Forms.Label.1", "lbl" & r & "_" & c, _
                       10 + (c - 1) * 30, 10 + r * 20, 28, 18, ""
        Next
    Next
    
    frm.Properties("Caption") = "カレンダー入力"
End Sub
VB

4-2: プログレスバー

Sub BuildUI_Progress(frm As VBComponent)
    AddControl frm, "Forms.Label.1", "lblBar", 10, 30, 0, 20, ""
    AddControl frm, "Forms.Label.1", "lblMsg", 10, 10, 200, 15, "処理中…"
    frm.Properties("Caption") = "Progress"
End Sub
VB

4-3: 検索ダイアログ

Sub BuildUI_Search(frm As VBComponent)
    AddControl frm, "Forms.TextBox.1", "txtKey", 10, 10, 150, 20
    AddControl frm, "Forms.CommandButton.1", "btnSearch", 170, 10, 60, 20, "検索"
    AddControl frm, "Forms.ListBox.1", "lstResult", 10, 40, 220, 140
    
    frm.Properties("Caption") = "検索ダイアログ"
End Sub
VB

4-4: ログイン画面

Sub BuildUI_Login(frm As VBComponent)
    AddControl frm, "Forms.Label.1", "lblU", 10, 10, 60, 18, "ユーザー名"
    AddControl frm, "Forms.TextBox.1", "txtUser", 80, 10, 120, 20
    
    AddControl frm, "Forms.Label.1", "lblP", 10, 40, 60, 18, "パスワード"
    AddControl frm, "Forms.TextBox.1", "txtPass", 80, 40, 120, 20
    
    AddControl frm, "Forms.CommandButton.1", "btnOK", 40, 75, 60, 24, "OK"
    AddControl frm, "Forms.CommandButton.1", "btnCancel", 120, 75, 60, 24, "Cancel"
    
    frm.Properties("Caption") = "ログイン"
End Sub
VB

4-5: 一覧 → 詳細画面

Sub BuildUI_ListDetail(frm As VBComponent)
    AddControl frm, "Forms.ListBox.1", "lst", 10, 10, 200, 150
    AddControl frm, "Forms.CommandButton.1", "btnDetail", 220, 10, 80, 25, "詳細を見る"
    
    frm.Properties("Caption") = "一覧画面"
End Sub
VB

4-6: 行編集フォーム(Add/Edit/Delete)

Sub BuildUI_Edit(frm As VBComponent)
    Dim labels: labels = Array("ID", "名前", "数量")
    Dim i As Long
    
    For i = 0 To 2
        AddControl frm, "Forms.Label.1", "lbl" & i, 10, 10 + i * 30, 40, 18, labels(i)
        AddControl frm, "Forms.TextBox.1", "txt" & i, 60, 10 + i * 30, 120, 20
    Next
    
    AddControl frm, "Forms.CommandButton.1", "btnAdd", 200, 10, 80, 25, "追加"
    AddControl frm, "Forms.CommandButton.1", "btnEdit", 200, 45, 80, 25, "更新"
    AddControl frm, "Forms.CommandButton.1", "btnDel", 200, 80, 80, 25, "削除"
    
    frm.Properties("Caption") = "行編集"
End Sub
VB

4-7: ドロップダウン一覧 UI

Sub BuildUI_DropFilter(frm As VBComponent)
    AddControl frm, "Forms.ComboBox.1", "cmb", 10, 10, 120, 20
    AddControl frm, "Forms.ListBox.1", "lst", 10, 40, 200, 140
    frm.Properties("Caption") = "ドロップダウンフィルタ"
End Sub
VB

4-8: 高機能検索(AND/OR)

Sub BuildUI_Adv(frm As VBComponent)
    AddControl frm, "Forms.TextBox.1", "txt1", 10, 10, 100, 20
    AddControl frm, "Forms.TextBox.1", "txt2", 120, 10, 100, 20
    
    AddControl frm, "Forms.OptionButton.1", "optAnd", 10, 40, 60, 18, "AND"
    AddControl frm, "Forms.OptionButton.1", "optOr", 80, 40, 60, 18, "OR"
    
    AddControl frm, "Forms.CommandButton.1", "btnSearch", 10, 70, 60, 25, "検索"
    AddControl frm, "Forms.ListBox.1", "lst", 10, 100, 200, 120
    
    frm.Properties("Caption") = "高機能検索"
End Sub
VB

5. 生成処理のメイン(BuildUI)

Sub BuildUI(uiType As String)
    Dim frm As VBComponent
    Dim name As String
    
    name = "UI_" & Format(Now, "yyyymmdd_hhnnss")
    Set frm = CreateNewForm(name)

    Select Case uiType
        Case "カレンダー入力": BuildUI_Calendar frm
        Case "日付範囲ピッカー": BuildUI_Calendar frm '※ベース使い回し可
        Case "プログレスバー": BuildUI_Progress frm
        Case "検索ダイアログ": BuildUI_Search frm
        Case "ログイン画面": BuildUI_Login frm
        Case "一覧 → 詳細画面": BuildUI_ListDetail frm
        Case "行編集フォーム(Add/Edit/Delete)": BuildUI_Edit frm
        Case "ドロップダウン一覧 UI": BuildUI_DropFilter frm
        Case "高機能検索フォーム(AND/OR 条件)": BuildUI_Adv frm
    End Select
    
    MsgBox "UI を自動生成しました:" & frm.Name
End Sub
VB

6. このツールでできること

  • ボタン一発で新しい UI フォームが自動作成
  • コントロールがレイアウト済み
  • Caption・サイズ・位置も調整済み
  • 名前衝突なし
  • すぐ編集して使える
タイトルとURLをコピーしました