CSV3

```
Sub ProcessPatterns()
    Dim wsConfig As Worksheet
    Dim wsOutput As Worksheet
    Dim wsMaster As Worksheet
    Dim wsTemp As Worksheet
    Dim configRow As Long
    Dim lastConfigRow As Long
    Dim masterRow As Long
    Dim lastMasterRow As Long
    Dim searchText As String
    Dim foundCell As Range
    Dim contractCell As Range
    Dim pasteRow As Long
    Dim sheetName As String
    Dim startCol As Long
    Dim masterNumber As String
    Dim lastRowOutput As Long
    Dim lastRowA As Long, lastRowB As Long, lastRowC As Long
    Dim searchTextLeftValue As String
    Dim testCodeCol As Long
    Dim testCodeCell As Range

    ' configシートを参照
    Set wsConfig = ThisWorkbook.Sheets("config")

    ' 出力シートを最初にクリア
    For Each wsTemp In ThisWorkbook.Sheets
        If Left(wsTemp.Name, 3) = "出力_" Then
            ' 出力シートの3行目以降をクリア
            wsTemp.Rows("3:" & wsTemp.Rows.Count).ClearContents
        End If
    Next wsTemp

    ' configシートのD列の最終行を取得
    lastConfigRow = wsConfig.Cells(wsConfig.Rows.Count, "D").End(xlUp).Row

    ' configシートのD列を上から順に処理
    For configRow = 2 To lastConfigRow
        ' 検索条件を取得
        searchText = wsConfig.Cells(configRow, "D").Value

        If searchText = "" Then Exit For ' 空白セルで終了

        ' 検索条件の左隣のセルの値を取得
        searchTextLeftValue = wsConfig.Cells(configRow, "C").Value

        ' すべてのシートをループして「マスタ_」で始まるシートを探す
        For Each wsMaster In ThisWorkbook.Sheets
            If Left(wsMaster.Name, 4) = "マスタ_" Then

                ' マスタシートの番号部分を抽出
                masterNumber = Split(wsMaster.Name, "_")(1)
                masterNumber = Split(masterNumber, ".")(0) ' "マスタ_1.テスト" なら "1" を取得

                ' 対応する出力シートを探す
                Set wsOutput = Nothing
                For Each wsTemp In ThisWorkbook.Sheets
                    If Left(wsTemp.Name, Len("出力_" & masterNumber)) = "出力_" & masterNumber Then
                        Set wsOutput = wsTemp
                        Exit For
                    End If
                Next wsTemp

                If wsOutput Is Nothing Then
                    MsgBox "出力シート 出力_" & masterNumber & " が見つかりません。", vbExclamation
                    Exit Sub
                End If

                ' マスタシートのA列、B列、C列を基に最終行を取得
                lastRowA = wsMaster.Cells(wsMaster.Rows.Count, "A").End(xlUp).Row
                lastRowB = wsMaster.Cells(wsMaster.Rows.Count, "B").End(xlUp).Row
                lastRowC = wsMaster.Cells(wsMaster.Rows.Count, "C").End(xlUp).Row
                lastMasterRow = Application.Max(lastRowA, lastRowB, lastRowC)

                ' マスタシートの2行目で検索条件を探す
                Set foundCell = wsMaster.Rows(2).Find(What:=searchText, LookIn:=xlValues, LookAt:=xlWhole)

                If Not foundCell Is Nothing Then

                    ' 「契約のみ」列を特定
                    Set contractCell = wsMaster.Rows(2).Find(What:="契約のみ", LookIn:=xlValues, LookAt:=xlWhole)
                    If Not contractCell Is Nothing Then
                        startCol = contractCell.Column

                        If Not foundCell Is Nothing Then
                            ' 初期貼り付け位置を計算
                            If wsOutput.Cells(wsOutput.Rows.Count, "A").End(xlUp).Row < 3 Then
                                pasteRow = 3
                            Else
                                pasteRow = wsOutput.Cells(wsOutput.Rows.Count, "A").End(xlUp).Row + 1
                            End If

                            ' マスタシートで "●" の行を出力シートに貼り付け
                            For masterRow = 3 To lastMasterRow
                                If wsMaster.Cells(masterRow, foundCell.Column).Value = "●" Then

                                    ' コピー対象をそのまま出力シートに貼り付け
                                    wsOutput.Cells(pasteRow, "A").Resize(, startCol - 1).Value = _
                                        wsMaster.Cells(masterRow, "A").Resize(, startCol - 1).Value

                                    ' 次の貼り付け行を更新
                                    pasteRow = pasteRow + 1

                                    ' 「テストコード」の列を特定
                                    Set testCodeCell = wsOutput.Rows(1).Find(What:="テストコード", LookIn:=xlValues, LookAt:=xlWhole)
                                    If Not testCodeCell Is Nothing Then
                                        testCodeCol = testCodeCell.Column
                                        ' 検索条件の左隣の値を貼り付け
                                        wsOutput.Cells(pasteRow - 1, testCodeCol).Value = searchTextLeftValue
                                    End If

                                End If
                            Next masterRow
                        End If
                    End If
                End If
            End If
        Next wsMaster
    Next configRow

    MsgBox "処理が完了しました。"
End Sub

```