0% found this document useful (0 votes)
2 views2 pages

Sub CopySheetsFromMultipleFiles

This VBA macro allows users to select multiple Excel files and merges all worksheets from those files into the current workbook. It opens each selected workbook, copies each worksheet to the target workbook, and then closes the source workbook without saving changes. A message box confirms the completion of the merging process.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Sub CopySheetsFromMultipleFiles

This VBA macro allows users to select multiple Excel files and merges all worksheets from those files into the current workbook. It opens each selected workbook, copies each worksheet to the target workbook, and then closes the source workbook without saving changes. A message box confirms the completion of the merging process.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Sub CopySheetsFromMultipleFiles()

Dim SourceWb As Workbook

Dim TargetWb As Workbook

Dim ws As Worksheet

Dim fileNames As Variant

Dim i As Long

' Let user pick multiple files

fileNames = [Link]( _

FileFilter:="Excel Files (*.xlsx), *.xlsx", _

Title:="Select workbooks to merge", _

MultiSelect:=True)

' Exit if no file is selected

If VarType(fileNames) = vbBoolean Then Exit Sub

Set TargetWb = ThisWorkbook

' Loop through each selected file

For i = LBound(fileNames) To UBound(fileNames)

Set SourceWb = [Link](fileNames(i))

' Copy each sheet

For Each ws In [Link]

[Link] After:=[Link]([Link])
Next ws

[Link] SaveChanges:=False

Next i

MsgBox " All sheets from selected files have been merged."

End Sub

You might also like