Sub MergeSelectedFiles()
    Dim wsTarget As Worksheet
    Dim wsSource As Worksheet
    Dim wbSource As Workbook
    Dim FileNames As Variant
    Dim FileName As Variant
    Dim TargetRow As Long
    Dim SourceRange As Range
    Dim LastRow As Long

    ' 현재 워크북의 첫 번째 시트 설정
    Set wsTarget = ThisWorkbook.Sheets(1)
    TargetRow = wsTarget.Cells(wsTarget.Rows.Count, 1).End(xlUp).Row + 1

    ' 파일 열기 대화상자 표시
    FileNames = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls; *.xlsx; *.xlsm), *.xls; *.xlsx; *.xlsm", MultiSelect:=True, Title:="Select Files to Merge")

    ' 파일이 선택되지 않은 경우 종료
    If IsArray(FileNames) = False Then Exit Sub

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    ' 선택한 파일 반복
    For Each FileName In FileNames
        Set wbSource = Workbooks.Open(FileName)

        ' 소스 파일의 모든 시트를 반복
        For Each wsSource In wbSource.Sheets
            ' 데이터 범위 설정 (1행 제외)
            LastRow = wsSource.Cells(wsSource.Rows.Count, 1).End(xlUp).Row
            If LastRow > 1 Then
                Set SourceRange = wsSource.Range("A2", wsSource.Cells(LastRow, wsSource.Cells(1, wsSource.Columns.Count).End(xlToLeft).Column))
                SourceRange.Copy
                wsTarget.Cells(TargetRow, 1).PasteSpecial Paste:=xlPasteValues
                TargetRow = wsTarget.Cells(wsTarget.Rows.Count, 1).End(xlUp).Row + 1
            End If
        Next wsSource

        ' 소스 파일 닫기
        wbSource.Close SaveChanges:=False
    Next FileName

    Application.CutCopyMode = False
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    MsgBox "Selected files have been successfully merged!", vbInformation
End Sub