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

Merge Excel Files

This document provides a VBA macro code to merge multiple Excel files into one file. The macro allows the user to select multiple workbooks using a file dialog box. It then loops through each selected file, opens it, copies all worksheets to the end of the main workbook, and closes the source workbook. Once complete, all worksheets from the selected files are combined into a single main workbook file.

Uploaded by

jonnie myers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views2 pages

Merge Excel Files

This document provides a VBA macro code to merge multiple Excel files into one file. The macro allows the user to select multiple workbooks using a file dialog box. It then loops through each selected file, opens it, copies all worksheets to the end of the main workbook, and closes the source workbook. Once complete, all worksheets from the selected files are combined into a single main workbook file.

Uploaded by

jonnie myers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Merge Excel Files: How to Combine Workbooks into One File

Merge files with a simple VBA macro

Copy and paste the following code into the new VBA module. Position the cursor within the code and click start (the green triangle) on the top. That’s it!

Sub mergeFiles()

'Merges all files in a folder to a main file.

'Define variables:

Dim numberOfFilesChosen, i As Integer

Dim tempFileDialog As fileDialog

Dim mainWorkbook, sourceWorkbook As Workbook

Dim tempWorkSheet As Worksheet

Set mainWorkbook = Application.ActiveWorkbook

Set tempFileDialog = Application.fileDialog(msoFileDialogFilePicker)

'Allow the user to select multiple workbooks

tempFileDialog.AllowMultiSelect = True

numberOfFilesChosen = tempFileDialog.Show

'Loop through all selected workbooks

For i = 1 To tempFileDialog.SelectedItems.Count

'Open each workbook

Workbooks.Open tempFileDialog.SelectedItems(i)

Set sourceWorkbook = ActiveWorkbook

'Copy each worksheet to the end of the main workbook

For Each tempWorkSheet In sourceWorkbook.Worksheets

tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)

Next tempWorkSheet

'Close the source workbook

sourceWorkbook.Close

Next i

End Sub

You might also like