■■■■■■■第4章 03■■■■■■■

■■■■p96プロンプト例

あなたは優秀なExcel VBAのプログラマーです。
次の操作をするExcel VBAのプログラムを書いてください。

＃＃＃＃現在の状態
・同じ形式の複数の表が縦に並んでいる 
・表の1行目には「紅茶」といった分類名が記載されている
・表の2行目は「種類」「産地」「価格」という項目名
・それぞれの表の行数はバラバラ

＃＃表の例
紅茶		
種類	産地	価格
ダージリン	インド	1500
アールグレイ	スリランカ	1000

＃＃＃＃プログラムの実行結果
・すべての表を1つに結合する
・結合した表は「結合後」シートに出力する
・マクロ名は「表の統合」とする

＃＃結果の例 
分類	種類	産地	価格
紅茶	ダージリン	インド	1500
紅茶	アールグレイ	スリランカ	1000


■■■■p99コード例

Sub 表の統合()
    Dim wsSrc As Worksheet
    Dim wsDst As Worksheet
    Dim lastRow As Long
    Dim srcRow As Long
    Dim dstRow As Long
    Dim category As String
    
    ' 元データはアクティブシートと仮定
    Set wsSrc = ActiveSheet
    
    ' 出力先シートを用意
    On Error Resume Next
    Set wsDst = ThisWorkbook.Sheets("結合後")
    If wsDst Is Nothing Then
        Set wsDst = ThisWorkbook.Sheets.Add
        wsDst.Name = "結合後"
    Else
        wsDst.Cells.Clear
    End If
    On Error GoTo 0
    
    ' 見出しを出力
    wsDst.Range("A1").Value = "分類"
    wsDst.Range("B1").Value = "種類"
    wsDst.Range("C1").Value = "産地"
    wsDst.Range("D1").Value = "価格"
    dstRow = 2
    
    ' 元データの最終行を取得
    lastRow = wsSrc.Cells(wsSrc.Rows.Count, "A").End(xlUp).Row
    srcRow = 1
    
    ' 表ごとに処理
    Do While srcRow <= lastRow
        ' 分類名（1行目）
        category = wsSrc.Cells(srcRow, "A").Value
        
        ' 項目行の次の行からデータをコピー
        srcRow = srcRow + 2 ' 項目行を飛ばす
        
        ' データ行を読み込み（空行または分類名で終了）
        Do While srcRow <= lastRow And wsSrc.Cells(srcRow, "A").Value <> ""
            ' 分類列を追加して出力
            wsDst.Cells(dstRow, "A").Value = category
            wsDst.Cells(dstRow, "B").Value = wsSrc.Cells(srcRow, "A").Value
            wsDst.Cells(dstRow, "C").Value = wsSrc.Cells(srcRow, "B").Value
            wsDst.Cells(dstRow, "D").Value = wsSrc.Cells(srcRow, "C").Value
            
            dstRow = dstRow + 1
            srcRow = srcRow + 1
        Loop
        
        ' 空行をスキップ
        Do While srcRow <= lastRow And wsSrc.Cells(srcRow, "A").Value = ""
            srcRow = srcRow + 1
        Loop
    Loop
    
    ' 列幅を自動調整
    wsDst.Columns.AutoFit
    
    MsgBox "表の統合が完了しました。", vbInformation
End Sub


■■■■p101プロンプト例

結果の表には、次の書式を加えてください。
・1行目の見出しは薄い緑色で塗りつぶす
・表全体に格子の罫線を引く
・「価格」列は3桁区切りにして、もう少し幅を広くする


■■■■p101コード例

Sub 表の統合()
    Dim wsSrc As Worksheet
    Dim wsDst As Worksheet
    Dim lastRow As Long
    Dim srcRow As Long
    Dim dstRow As Long
    Dim category As String
    Dim lastDstRow As Long
    
    ' 元データはアクティブシートと仮定
    Set wsSrc = ActiveSheet
    
    ' 出力先シートを用意
    On Error Resume Next
    Set wsDst = ThisWorkbook.Sheets("結合後")
    If wsDst Is Nothing Then
        Set wsDst = ThisWorkbook.Sheets.Add
        wsDst.Name = "結合後"
    Else
        wsDst.Cells.Clear
    End If
    On Error GoTo 0
    
    ' 見出しを出力
    wsDst.Range("A1").Value = "分類"
    wsDst.Range("B1").Value = "種類"
    wsDst.Range("C1").Value = "産地"
    wsDst.Range("D1").Value = "価格"
    dstRow = 2
    
    ' 元データの最終行を取得
    lastRow = wsSrc.Cells(wsSrc.Rows.Count, "A").End(xlUp).Row
    srcRow = 1
    
    ' 表ごとに処理
    Do While srcRow <= lastRow
        ' 分類名（1行目）
        category = wsSrc.Cells(srcRow, "A").Value
        
        ' 項目行の次の行からデータをコピー
        srcRow = srcRow + 2 ' 項目行を飛ばす
        
        ' データ行を読み込み（空行または分類名で終了）
        Do While srcRow <= lastRow And wsSrc.Cells(srcRow, "A").Value <> ""
            ' 分類列を追加して出力
            wsDst.Cells(dstRow, "A").Value = category
            wsDst.Cells(dstRow, "B").Value = wsSrc.Cells(srcRow, "A").Value
            wsDst.Cells(dstRow, "C").Value = wsSrc.Cells(srcRow, "B").Value
            wsDst.Cells(dstRow, "D").Value = wsSrc.Cells(srcRow, "C").Value
            
            dstRow = dstRow + 1
            srcRow = srcRow + 1
        Loop
        
        ' 空行をスキップ
        Do While srcRow <= lastRow And wsSrc.Cells(srcRow, "A").Value = ""
            srcRow = srcRow + 1
        Loop
    Loop
    
    ' 最終行取得
    lastDstRow = wsDst.Cells(wsDst.Rows.Count, "A").End(xlUp).Row
    
    ' 1行目見出しを薄い緑色で塗りつぶす (色はRGBで調整)
    With wsDst.Range("A1:D1").Interior
        .Pattern = xlSolid
        .Color = RGB(198, 239, 206) ' Excelの薄い緑色（近似）
    End With
    
    ' 表全体に格子の罫線を引く
    With wsDst.Range("A1:D" & lastDstRow).Borders
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    
    ' 「価格」列を3桁区切りにして幅を広げる
    With wsDst.Range("D2:D" & lastDstRow)
        .NumberFormat = "#,##0"
    End With
    wsDst.Columns("D").ColumnWidth = 12
    
    ' 他の列も自動調整
    wsDst.Columns("A:C").AutoFit
    
    MsgBox "表の統合と書式設定が完了しました。", vbInformation
End Sub
