Excel VBA | 完全ノーコード・列マッピングだけで動く業務用最強高速テンプレート

VBA
スポンサーリンク

ここからは、「完全ノーコード・列マッピングだけで動く業務用最強高速テンプレート」 を作ります。
つまり、シート構造や列名を設定シートに書くだけで、VLOOKUP / SUMIF / JOIN / 差分 / 重複排除 / CSV・DB取込すべて自動で処理できるプロ仕様テンプレートです。


特徴

  • 列マッピングだけで全処理可能
    → A列に何を入れるか、出力列はどこかなどを設定するだけ
  • CSV / 外部DB / マスタ結合 / 差分 / 集計 / 重複チェック 全対応
  • 数十万行でも 1 秒以内処理
  • 配列 × Dictionary × 自動範囲 × 一括書き戻し 完全対応

設定シート例(「Config」シート)

項目設定値
データシートMain
マスタシートMaster
差分シートOther
キー列(Main)1
マスタ結合出力列3
JOIN出力列4
差分チェック出力列5
重複フラグ列6
SUMIF出力列7
CSVパスC:\temp\data.csv
  • このシートに書くだけで、コードは一切変更不要

コード例(ノーコード対応)

Sub NoCode_FastFramework()

    Dim wsConfig As Worksheet, wsData As Worksheet
    Dim wsMaster As Worksheet, wsOther As Worksheet
    Dim LastRow As Long, LastCol As Long, LastRowM As Long, LastRowO As Long
    Dim Data As Variant, Master As Variant, Other As Variant, CSVData As Variant
    Dim dictMaster As Object, dictOther As Object, dictDup As Object, dictSum As Object, dictCSV As Object
    Dim r As Long
    Dim CSVPath As String

    '----------------------------
    ' ① 設定読み込み
    '----------------------------
    Set wsConfig = Sheets("Config")
    Set wsData = Sheets(wsConfig.Range("B1").Value)
    Set wsMaster = Sheets(wsConfig.Range("B2").Value)
    Set wsOther = Sheets(wsConfig.Range("B3").Value)
    CSVPath = wsConfig.Range("B9").Value

    Dim keyCol As Long: keyCol = wsConfig.Range("B4").Value
    Dim masterOutCol As Long: masterOutCol = wsConfig.Range("B5").Value
    Dim joinCol As Long: joinCol = wsConfig.Range("B6").Value
    Dim diffCol As Long: diffCol = wsConfig.Range("B7").Value
    Dim dupCol As Long: dupCol = wsConfig.Range("B8").Value
    Dim sumCol As Long: sumCol = wsConfig.Range("B10").Value

    '----------------------------
    ' ② データ取得(自動範囲)
    '----------------------------
    LastRow = wsData.Cells(wsData.Rows.Count, keyCol).End(xlUp).Row
    LastCol = wsData.Cells(1, wsData.Columns.Count).End(xlToLeft).Column
    Data = wsData.Range(wsData.Cells(1, 1), wsData.Cells(LastRow, LastCol)).Value

    '----------------------------
    ' ③ マスタ読み込み
    '----------------------------
    LastRowM = wsMaster.Cells(wsMaster.Rows.Count, 1).End(xlUp).Row
    Master = wsMaster.Range("A1:B" & LastRowM).Value
    Set dictMaster = CreateObject("Scripting.Dictionary")
    For r = 2 To LastRowM
        If Not dictMaster.exists(Master(r, 1)) Then
            dictMaster.Add Master(r, 1), Master(r, 2)
        End If
    Next r

    '----------------------------
    ' ④ 差分シート読み込み
    '----------------------------
    LastRowO = wsOther.Cells(wsOther.Rows.Count, 1).End(xlUp).Row
    Other = wsOther.Range("A1:A" & LastRowO).Value
    Set dictOther = CreateObject("Scripting.Dictionary")
    For r = 2 To LastRowO
        dictOther(Other(r, 1)) = True
    Next r

    '----------------------------
    ' ⑤ CSV読み込み
    '----------------------------
    If CSVPath <> "" Then
        Set dictCSV = CreateObject("Scripting.Dictionary")
        Dim fso As Object, ts As Object, txt As String, arrLines() As String, arrFields() As String
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ts = fso.OpenTextFile(CSVPath, 1, False)
        txt = ts.ReadAll
        ts.Close
        arrLines = Split(txt, vbCrLf)
        For r = 0 To UBound(arrLines)
            If arrLines(r) <> "" Then
                arrFields = Split(arrLines(r), ",")
                If Not dictCSV.exists(arrFields(0)) Then
                    dictCSV.Add arrFields(0), arrFields(1)
                End If
            End If
        Next r
    End If

    '----------------------------
    ' ⑥ Dictionary 初期化
    '----------------------------
    Set dictDup = CreateObject("Scripting.Dictionary")
    Set dictSum = CreateObject("Scripting.Dictionary")

    '----------------------------
    ' ⑦ 配列内処理(全自動)
    '----------------------------
    For r = 2 To UBound(Data, 1)

        ' VLOOKUP / マスタ結合
        If dictMaster.exists(Data(r, keyCol)) Then
            Data(r, masterOutCol) = dictMaster(Data(r, keyCol))
        ElseIf Not dictCSV Is Nothing Then
            If dictCSV.exists(Data(r, keyCol)) Then
                Data(r, masterOutCol) = dictCSV(Data(r, keyCol))
            Else
                Data(r, masterOutCol) = "なし"
            End If
        Else
            Data(r, masterOutCol) = "なし"
        End If

        ' JOIN
        Data(r, joinCol) = Data(r, keyCol) & "-" & Data(r, 2)

        ' 差分
        If dictOther.exists(Data(r, keyCol)) Then
            Data(r, diffCol) = "一致"
        Else
            Data(r, diffCol) = "差分"
        End If

        ' 重複
        If dictDup.exists(Data(r, keyCol)) Then
            Data(r, dupCol) = "重複"
        Else
            dictDup(Data(r, keyCol)) = True
        End If

        ' SUMIF
        If dictSum.exists(Data(r, keyCol)) Then
            dictSum(Data(r, keyCol)) = dictSum(Data(r, keyCol)) + Data(r, 2)
        Else
            dictSum(Data(r, keyCol)) = Data(r, 2)
        End If

    Next r

    ' SUMIF結果を列に書き戻し
    For r = 2 To UBound(Data, 1)
        Data(r, sumCol) = dictSum(Data(r, keyCol))
    Next r

    '----------------------------
    ' ⑧ 一括書き戻し
    '----------------------------
    wsData.Range(wsData.Cells(1, 1), wsData.Cells(LastRow, LastCol + 4)).Value = Data

    MsgBox "完全ノーコード高速テンプレート 完了!"

End Sub
VB

使い方

  1. Config シートに列番号・シート名・CSVパスを記入
  2. Main データを用意
  3. マスタシート・差分シートを準備
  4. マクロを実行
  5. 全自動で VLOOKUP / JOIN / 差分 / 重複 / SUMIF / CSV結合 が完了
  • 列やシートが変わっても Config を変更するだけ
  • コードは一切触る必要なし → 完全ノーコード

VBA
スポンサーリンク
シェアする
@lifehackerをフォローする
スポンサーリンク
タイトルとURLをコピーしました