hiko-blog

VBA業務改善

MENU

結合シート作成

Sub 結合シート作成()
    Dim ws As Worksheet
    Dim combinedSheet As Worksheet
    Dim lastRow As Long
    Dim combinedRow As Long
    
    ' 新しいシートを作成して、結合先として使用します
    Set combinedSheet = ThisWorkbook.Sheets.Add(After:= _
             ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    combinedSheet.Name = "結合シート"
    
    ' 最初のシートからデータをコピーして貼り付けます
    For Each ws In ThisWorkbook.Sheets
        If ws.Name <> combinedSheet.Name Then
            lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
            If lastRow > 1 Then
                ws.Range("A2:A" & lastRow).EntireRow.Copy
                combinedSheet.Cells(combinedSheet.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
                Application.CutCopyMode = False
            End If
        End If
    Next ws
    
    MsgBox "シートが結合されました。", vbInformation
End Sub