You are on page 1of 21

4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

This documentation is archived and is not being maintained.

Merging Data from Multiple Workbooks into a Summary


Workbook in Excel
Office 2007

Summary: Microsoft Office Excel MVP Ron de Bruin provides a number of samples and a handy addin to merge data from multiple workbooks located in one folder into
a summary workbook. 13 printed pages

Ron de Bruin, Microsoft Office Excel MVP

Frank Rice, Microsoft Corporation

August 2008

Applies to:Microsoft Office Excel 2007, Microsoft Office Excel 2003, Microsoft Excel 2002, Microsoft Excel 2000

Contents

Overview

Finding the Last Cell, Column, or Row in a Range

Merging a Range from All Workbooks in a Folder

Merging a Range from Selected Workbooks

Merging a Range from Multiple Workbooks by Column

Merging a Range from Multiple Workbooks in a Folder with a Filter

More Options for Working with Workbooks

The RDBMerge Utility

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 1/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

Conclusion

This documentation
Additional Resources is archived and is not being maintained.

About the Authors

Overview
When working with multiple Microsoft Office Excel workbooks, a common task is to rollup or merge the data in each workbook into a master workbook. The examples
described in this article add the data from multiple workbooks to a summary workbook. The different procedures demonstrate techniques for pasting the data by row
or by column. Additionally, you will see how to retrieve data by using a filter. And finally, you will see a utility that pulls all of these techniques together and more in one
location.

You can download workbooks containing the code in this article at Ron de Bruin's Web site.

Finding the Last Cell, Column, or Row in a Range


The following code is used in some of the examples in this article.

To find the last cell, column, or row in a range

1. Open a new workbook in Excel.

2. Press Alt+F11 to open the Visual Basic Editor.

3. On the Insert menu, click Module to add a module to the workbook.

4. In the module window, type or paste the following function and then press Alt+Q to close the Visual Basic Editor.

VB

FunctionRDB_Last(choiceAsInteger,rngAsRange)
'ByRondeBruin,5May2008
'Achoiceof1=lastrow.
'Achoiceof2=lastcolumn.
'Achoiceof3=lastcell.
DimlrwAsLong

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 2/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel
DimlcolAsInteger

SelectCasechoice
This documentation is archived and is not being maintained.
Case1:
OnErrorResumeNext
RDB_Last=rng.Find(What:="*",_
after:=rng.cells(1),_
Lookat:=xlPart,_
LookIn:=xlFormulas,_
SearchOrder:=xlByRows,_
SearchDirection:=xlPrevious,_
MatchCase:=False).Row
OnErrorGoTo0

Case2:
OnErrorResumeNext
RDB_Last=rng.Find(What:="*",_
after:=rng.cells(1),_
Lookat:=xlPart,_
LookIn:=xlFormulas,_
SearchOrder:=xlByColumns,_
SearchDirection:=xlPrevious,_
MatchCase:=False).Column
OnErrorGoTo0

Case3:
OnErrorResumeNext
lrw=rng.Find(What:="*",_
after:=rng.cells(1),_
Lookat:=xlPart,_
LookIn:=xlFormulas,_
SearchOrder:=xlByRows,_
SearchDirection:=xlPrevious,_
MatchCase:=False).Row
OnErrorGoTo0

OnErrorResumeNext
lcol=rng.Find(What:="*",_
after:=rng.cells(1),_
Lookat:=xlPart,_
LookIn:=xlFormulas,_
SearchOrder:=xlByColumns,_

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 3/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

SearchDirection:=xlPrevious,_
MatchCase:=False).Column
OnErrorGoTo0
This documentation is archived and is not being maintained.
OnErrorResumeNext
RDB_Last=rng.Parent.cells(lrw,lcol).Address(False,False)
IfErr.Number>0Then
RDB_Last=rng.cells(1).Address(False,False)
Err.Clear
EndIf
OnErrorGoTo0

EndSelect
EndFunction

This function uses the Range object's Find method to search for the last item in the workbook depending on the value of the choice argument. The choice argument
specifies a cell, column, or row.

Merging a Range from All Workbooks in a Folder


To merge data from all workbooks in a folder, type or paste the following code in standard module in the Visual Basic Editor. The ranges are concatenated into the
target worksheet, one after another, in rows.

VB

SubMergeAllWorkbooks()
DimMyPathAsString,FilesInPathAsString
DimMyFiles()AsString
DimSourceRcountAsLong,FNumAsLong
DimmybookAsWorkbook,BaseWksAsWorksheet
DimsourceRangeAsRange,destrangeAsRange
DimrnumAsLong,CalcModeAsLong

'Changethistothepath\folderlocationofyourfiles.
MyPath="C:\Users\Ron\test"

'Addaslashattheendofthepathifneeded.
IfRight(MyPath,1)<>"\"Then
MyPath=MyPath&"\"
https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 4/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

EndIf

'IftherearenoExcelfilesinthefolder,exit.
This documentation is archived and is not being maintained.
FilesInPath=Dir(MyPath&"*.xl*")
IfFilesInPath=""Then
MsgBox"Nofilesfound"
ExitSub
EndIf

'FillthemyFilesarraywiththelistofExcelfiles
'inthesearchfolder.
FNum=0
DoWhileFilesInPath<>""
FNum=FNum+1
ReDimPreserveMyFiles(1ToFNum)
MyFiles(FNum)=FilesInPath
FilesInPath=Dir()
Loop

'Setvariousapplicationproperties.
WithApplication
CalcMode=.Calculation
.Calculation=xlCalculationManual
.ScreenUpdating=False
.EnableEvents=False
EndWith

'Addanewworkbookwithonesheet.
SetBaseWks=Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum=1

'LoopthroughallfilesinthemyFilesarray.
IfFNum>0Then
ForFNum=LBound(MyFiles)ToUBound(MyFiles)
Setmybook=Nothing
OnErrorResumeNext
Setmybook=Workbooks.Open(MyPath&MyFiles(FNum))
OnErrorGoTo0

IfNotmybookIsNothingThen
OnErrorResumeNext

'Changethisrangetofityourownneeds.

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 5/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

Withmybook.Worksheets(1)
SetsourceRange=.Range("A1:C1")
EndWith
This documentation is archived and is not being maintained.
IfErr.Number>0Then
Err.Clear
SetsourceRange=Nothing
Else
'Ifsourcerangeusesallcolumnsthen
'skipthisfile.
IfsourceRange.Columns.Count>=BaseWks.Columns.CountThen
SetsourceRange=Nothing
EndIf
EndIf
OnErrorGoTo0

IfNotsourceRangeIsNothingThen

SourceRcount=sourceRange.Rows.Count

Ifrnum+SourceRcount>=BaseWks.Rows.CountThen
MsgBox"Therearenotenoughrowsinthetargetworksheet."
BaseWks.Columns.AutoFit
mybook.Closesavechanges:=False
GoToExitTheSub
Else

'CopythefilenameincolumnA.
WithsourceRange
BaseWks.Cells(rnum,"A")._
Resize(.Rows.Count).Value=MyFiles(FNum)
EndWith

'Setthedestinationrange.
Setdestrange=BaseWks.Range("B"&rnum)

'Copythevaluesfromthesourcerange
'tothedestinationrange.
WithsourceRange
Setdestrange=destrange._
Resize(.Rows.Count,.Columns.Count)
EndWith
destrange.Value=sourceRange.Value

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 6/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

rnum=rnum+SourceRcount
EndIf
This documentation is archived and is not being maintained.
EndIf
mybook.Closesavechanges:=False
EndIf

NextFNum
BaseWks.Columns.AutoFit
EndIf

ExitTheSub:
'Restoretheapplicationproperties.
WithApplication
.ScreenUpdating=True
.EnableEvents=True
.Calculation=CalcMode
EndWith
EndSub

This procedure fills an array with the path and name of each workbook in a folder. It then loops through the array and for each source file, checks the source and target
ranges to see if there are more columns used in the source range than are available in the target range. If this is true, then this workbook is skipped and the code moves
to the next workbook. The code then does the same test for the rows in the source range.

Next the procedure copies the path and name of the source workbook into column A. Finally, the values in the source range are copied into the corresponding range in
the target workbook and the code moves to the next file in the array.

This procedure uses the first worksheet index 1 of each workbook. To start with a different worksheet to use a specific worksheet, just change the index number or
change the index to the name of the worksheet.

VB

Withmybook.Worksheets("YourSheetName")

You will also likely want to change the range A1:C1 to your own values.

VB

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 7/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

Withmybook.Worksheets(1)
SetsourceRange=.Range("A1:C1")
This documentation is archived and is not being maintained.
EndWith

If you want to copy from cell A2 until the last cell on the worksheet then replace this code with the following code. You might do this if there are headers in the first
row.

Note

If you use this procedure, copy the function RDB_Last into your code module.

First, add this line at the top of the macro.

VB

DimFirstCellAsString

Then add this code.

VB

Withmybook.Worksheets(1)
FirstCell="A2"
SetsourceRange=.Range(FirstCell&":"&RDB_Last(3,.Cells))
'Testiftherowofthelastcellisequaltoorgreaterthantherowofthefirstcell.
IfRDB_Last(1,.Cells)<.Range(FirstCell).RowThen
SetsourceRange=Nothing
EndIf
EndWith

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 8/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

Merging a Range from Selected Workbooks


This documentation is archived and is not being maintained.
To merge data from specific workbooks, type or paste the following code in the module code window.

VB

PrivateDeclareFunctionSetCurrentDirectoryALib_
"kernel32"(ByVallpPathNameAsString)AsLong

SubChDirNet(szPathAsString)
SetCurrentDirectoryAszPath
EndSub

SubMergeSpecificWorkbooks()
DimMyPathAsString
DimSourceRcountAsLong,FNumAsLong
DimmybookAsWorkbook,BaseWksAsWorksheet
DimsourceRangeAsRange,destrangeAsRange
DimrnumAsLong,CalcModeAsLong
DimSaveDriveDirAsString
DimFNameAsVariant

'Setapplicationproperties.
WithApplication
CalcMode=.Calculation
.Calculation=xlCalculationManual
.ScreenUpdating=False
.EnableEvents=False
EndWith

SaveDriveDir=CurDir
'Changethistothepath\folderlocationofthefiles.
ChDirNet"C:\Users\Ron\test"

FName=Application.GetOpenFilename(filefilter:="ExcelFiles(*.xl*),*.xl*",_
MultiSelect:=True)
IfIsArray(FName)Then

'Addanewworkbookwithonesheet.

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 9/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

SetBaseWks=Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum=1
This documentation is archived and is not being maintained.
'LoopthroughallfilesinthemyFilesarray.
ForFNum=LBound(FName)ToUBound(FName)
Setmybook=Nothing
OnErrorResumeNext
Setmybook=Workbooks.Open(FName(FNum))
OnErrorGoTo0

IfNotmybookIsNothingThen

OnErrorResumeNext
Withmybook.Worksheets(1)
SetsourceRange=.Range("A1:C1")
EndWith

IfErr.Number>0Then
Err.Clear
SetsourceRange=Nothing
Else
'Ifthesourcerangeusesallcolumnsthen
'skipthisfile.
IfsourceRange.Columns.Count>=BaseWks.Columns.CountThen
SetsourceRange=Nothing
EndIf
EndIf
OnErrorGoTo0

IfNotsourceRangeIsNothingThen

SourceRcount=sourceRange.Rows.Count

Ifrnum+SourceRcount>=BaseWks.Rows.CountThen
MsgBox"Therearenotenoughrowsinthetargetworksheet."
BaseWks.Columns.AutoFit
mybook.Closesavechanges:=False
GoToExitTheSub
Else

'CopythefilenameincolumnA.
WithsourceRange

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 10/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

BaseWks.Cells(rnum,"A")._
Resize(.Rows.Count).Value=FName(FNum)
EndWith
This documentation is archived and is not being maintained.
'Setthedestinationrange.
Setdestrange=BaseWks.Range("B"&rnum)

'Copythevaluesfromthesourcerange
'tothedestinationrange.
WithsourceRange
Setdestrange=destrange._
Resize(.Rows.Count,.Columns.Count)
EndWith
destrange.Value=sourceRange.Value
rnum=rnum+SourceRcount
EndIf
EndIf
mybook.Closesavechanges:=False
EndIf

NextFNum
BaseWks.Columns.AutoFit
EndIf

ExitTheSub:
'Restoretheapplicationproperties.
WithApplication
.ScreenUpdating=True
.EnableEvents=True
.Calculation=CalcMode
EndWith
ChDirNetSaveDriveDir
EndSub

This code example will do the same thing as the first example only you are able to select the files you want to merge. The function ChDirNet is used so that you can set
the starting path to the network folder of your choice. You can also change the worksheet and range by using the changes described in the first example.

Merging a Range from Multiple Workbooks by Column


To paste data from source workbooks horizontally in columns in a target workbook, type or paste the following code in the module code window.
https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 11/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

VB

This documentation is archived and is not being maintained.


SubMergeHorizontally()
DimMyPathAsString,FilesInPathAsString
DimMyFiles()AsString
DimSourceCcountAsLong,FNumAsLong
DimmybookAsWorkbook,BaseWksAsWorksheet
DimsourceRangeAsRange,destrangeAsRange
DimCnumAsLong,CalcModeAsLong

'Changethistothepath\folderlocationofthefiles.
MyPath="C:\Users\Ron\test"

'Addaslashattheendofpathifneeded.
IfRight(MyPath,1)<>"\"Then
MyPath=MyPath&"\"
EndIf

'IftherearenoExcelfilesinthefolder,exit.
FilesInPath=Dir(MyPath&"*.xl*")
IfFilesInPath=""Then
MsgBox"Nofilesfound"
ExitSub
EndIf

'FillinthemyFilesarraywiththelistofExcelfilesin
'thesearchfolder.
FNum=0
DoWhileFilesInPath<>""
FNum=FNum+1
ReDimPreserveMyFiles(1ToFNum)
MyFiles(FNum)=FilesInPath
FilesInPath=Dir()
Loop

'Changetheapplicationproperties.
WithApplication
CalcMode=.Calculation
.Calculation=xlCalculationManual
.ScreenUpdating=False
.EnableEvents=False
EndWith

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 12/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

'Addanewworkbookwithonesheet.
SetBaseWks=Workbooks.Add(xlWBATWorksheet).Worksheets(1)
This documentation is archived and is not being maintained.
Cnum=1

'LoopthroughallofthefilesinthemyFilesarray.
IfFNum>0Then
ForFNum=LBound(MyFiles)ToUBound(MyFiles)
Setmybook=Nothing
OnErrorResumeNext
Setmybook=Workbooks.Open(MyPath&MyFiles(FNum))
OnErrorGoTo0

IfNotmybookIsNothingThen

OnErrorResumeNext
SetsourceRange=mybook.Worksheets(1).Range("A1:A10")

IfErr.Number>0Then
Err.Clear
SetsourceRange=Nothing
Else
'Ifthesourcerangeusesalloftherows
'thenskipthisfile.
IfsourceRange.Rows.Count>=BaseWks.Rows.CountThen
SetsourceRange=Nothing
EndIf
EndIf
OnErrorGoTo0

IfNotsourceRangeIsNothingThen

SourceCcount=sourceRange.Columns.Count

IfCnum+SourceCcount>=BaseWks.Columns.CountThen
MsgBox"Therearenotenoughcolumnsinthesheet."
BaseWks.Columns.AutoFit
mybook.Closesavechanges:=False
GoToExitTheSub
Else

'Copythefilenameinthefirstrow.
WithsourceRange

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 13/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

BaseWks.Cells(1,Cnum)._
Resize(,.Columns.Count).Value=MyFiles(FNum)
EndWith
This documentation is archived and is not being maintained.
'Setthedestinationrange.
Setdestrange=BaseWks.Cells(2,Cnum)

'Copythevaluesfromthesourcerange
'tothedestinationrange.
WithsourceRange
Setdestrange=destrange._
Resize(.Rows.Count,.Columns.Count)
EndWith
destrange.Value=sourceRange.Value

Cnum=Cnum+SourceCcount
EndIf
EndIf
mybook.Closesavechanges:=False
EndIf

NextFNum
BaseWks.Columns.AutoFit
EndIf

ExitTheSub:
'RestoreScreenUpdating,CalculationandEnableEvents
WithApplication
.ScreenUpdating=True
.EnableEvents=True
.Calculation=CalcMode
EndWith

EndSub

The following line is where columns are specified as the target as opposed to rows.

VB

Setdestrange=BaseWks.Cells(2,Cnum)

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 14/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

This documentation is archived and is not being maintained.


Merging a Range from Multiple Workbooks in a Folder with a Filter
To merge data retrieved based on a filter, type or paste the following code in the module code window.

VB

SubMergewithAutoFilter()
DimMyPathAsString,FilesInPathAsString
DimMyFiles()AsString
DimSourceRcountAsLong,FNumAsLong
DimmybookAsWorkbook,BaseWksAsWorksheet
DimsourceRangeAsRange,destrangeAsRange
DimrnumAsLong,CalcModeAsLong
DimrngAsRange,SearchValueAsString
DimFilterFieldAsInteger,RangeAddressAsString
DimShNameAsVariant,RwCountAsLong

'**************************************************************
'***Changethesefivelinesofcodebeforeyourunthemacro***
'**************************************************************

'Changethistothepath\folderlocationofthefiles.
MyPath="C:\Users\Ron\test"

'Fillinthenameofthesheetcontainingthedata.
'UseShName="SheetName"touseasheetnameinsteadifits
'index.Thisexampleusestheindexofthefirstsheetin
'everyworkbook.
ShName=1

'Fillinthefilterrange:A1istheheaderofthefirst
'columnandGisthelastcolumnintherangeandwill
'filteronallrowsonthesheet.
'YoucanalsouseafixedrangesuchasA1:G2500.
RangeAddress=Range("A1:G"&Rows.Count).Address

'Setthefieldthatyouwanttofilterintherange
'"1=columnA"inthisexamplebecausethefilterrange
'startsincolumnA.
FilterField=1

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 15/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

'Fillinthefiltervalue.Usethe"<>"ifyouwantto
'filterontheabsenceofaterm.Orusewildcardssuch
This documentation is archived and is not being maintained.
'as"ron*"forcellsthatstartwithron,oruse
'"*ron*"ifyoulookforcellswhereronisapartofthe
'cellvalue.
SearchValue="ron"

'**********************************************************
'**********************************************************

'AddaslashafterMyPathifneeded.
IfRight(MyPath,1)<>"\"Then
MyPath=MyPath&"\"
EndIf

'IftherearenoExcelfilesinthefolder,exit.
FilesInPath=Dir(MyPath&"*.xl*")
IfFilesInPath=""Then
MsgBox"Nofilesfound"
ExitSub
EndIf

'FillthemyFilesarraywiththelistofExcelfilesinthe
'folder.
FNum=0
DoWhileFilesInPath<>""
FNum=FNum+1
ReDimPreserveMyFiles(1ToFNum)
MyFiles(FNum)=FilesInPath
FilesInPath=Dir()
Loop

'Changeapplicationproperties.
WithApplication
CalcMode=.Calculation
.Calculation=xlCalculationManual
.ScreenUpdating=False
.EnableEvents=False
EndWith

'Addanewworkbookwithonesheet.

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 16/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

SetBaseWks=Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum=1
This documentation is archived and is not being maintained.
'LoopthroughallfilesinthemyFilesarray.
IfFNum>0Then
ForFNum=LBound(MyFiles)ToUBound(MyFiles)
Setmybook=Nothing
OnErrorResumeNext
Setmybook=Workbooks.Open(MyPath&MyFiles(FNum))
OnErrorGoTo0

IfNotmybookIsNothingThen

OnErrorResumeNext
'Setthefilterrange.
Withmybook.Worksheets(ShName)
SetsourceRange=.Range(RangeAddress)
EndWith

IfErr.Number>0Then
Err.Clear
SetsourceRange=Nothing
EndIf
OnErrorGoTo0

IfNotsourceRangeIsNothingThen
'Findthelastrowintargetworksheet.
rnum=RDB_Last(1,BaseWks.Cells)+1

WithsourceRange.Parent
Setrng=Nothing

'RemovetheAutoFilter.
.AutoFilterMode=False

'Filtertherangeonthe
'valueinfiltercolumn.
sourceRange.AutoFilterField:=FilterField,_
Criteria1:=SearchValue

With.AutoFilter.Range

'Checktoseeifthereareresults

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 17/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

'afterafterapplyingthefilter.
RwCount=.Columns(1).Cells._
SpecialCells(xlCellTypeVisible).Cells.Count1
This documentation is archived and is not being maintained.
IfRwCount=0Then
'Thereisnodata,onlythe
'header.
Else
'Setarangewithoutthe
'headerrow.
Setrng=.Resize(.Rows.Count1,.Columns.Count)._
Offset(1,0).SpecialCells(xlCellTypeVisible)

'Copytherangeandthefilename
'incolumnA.
Ifrnum+RwCount<BaseWks.Rows.CountThen
BaseWks.Cells(rnum,"A").Resize(RwCount).Value_
=mybook.Name
rng.CopyBaseWks.Cells(rnum,"B")
EndIf
EndIf

EndWith

'RemovetheAutoFilter
.AutoFilterMode=False

EndWith
EndIf

'Closetheworkbookwithoutsaving.
mybook.Closesavechanges:=False
EndIf

'Openthenextworkbook.
NextFNum

'Setthecolumnwidthinthenewworkbook.
BaseWks.Columns.AutoFit
MsgBox"Lookatthemergeresultsinthenewworkbook"&_
"afteryouclickonOK."
EndIf

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 18/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

'Restoretheapplicationproperties.
WithApplication
This documentation is archived and is not being maintained.
.ScreenUpdating=True
.EnableEvents=True
.Calculation=CalcMode
EndWith
EndSub

In this example, the following line of code is used to search for data matching the search term.

VB

sourceRange.AutoFilterField:=FilterField,Criteria1:=SearchValue

More Options for Working with Workbooks


In the previous paragraphs, four code examples for working for files in one folder were discussed. Minor changes to these examples can make them even more useful.
For example, if your workbooks are password protected, you can replace the Workbooks.Open arguments with the following code to open them.

VB

Setmybook=Workbooks.Open(MyPath&MyFiles(Fnum),_
Password:="ron",WriteResPassword:="ron",UpdateLinks:=0)

If you have links in your workbook to other workbooks, the setting UpdateLinks:=0 will avoid the message of whether you want to update the links. Use the value 3 if
you do want to update the links.

Another change you can make is to merge from all files with a name that starts with a specific name. For example, you can use the following statement to find all
workbooks that start with week.

VB

FilesInPath=Dir(MyPath&"week*.xl*")

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 19/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

You can find more information and code sample for merging the data in the subfolders and looping through all worksheets in every workbook at the following location
on Ron de Bruin's Web site.
This documentation is archived and is not being maintained.

The RDBMerge Utility


The RDBMerge utility provides a user friendly way to merge data from workbooks in a folder into one worksheet in a new workbook. Working with the addin is very
easy; however, for more information, see the page on Ron de Bruin's Web site.

To install the RDBMerge utility

1. Navigate to the RDBMerge utility page.

2. Download and extract the zip file to a local directory on your computer.

3. Copy either RDBMerge.xlam or RDBMerge.xla, depending on whether you are using the 2007 release of Microsoft Office or a previous version of Microsoft Office,
respectively, to the following directory:

local_drive:\Program Files\Microsoft Office\Version_Number\Library

Note

Depending on the version of Excel you are using, the Version_Number directory may be named just Office or may include a version number. For example:
local_drive:\Program Files\Microsoft Office\Office\Library or local_drive:\Program Files\Microsoft Office\Office11\Library.

Once the utility is installed, do the following to access it:

4. Start Excel and open a workbook.

5. Excel 2007 only Click the Microsoft Office button, click Excel Options, and then click the AddIns tab. In the Manage dropdown list, click Excel Addins, and
then click Go. Verify that RDBMerge is selected in this list and then click OK.

6. Excel 20002003 only Click Tools, click AddIns, verify RDBMerge is selected in the list, and then click OK.

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 20/21
4/5/2017 MergingDatafromMultipleWorkbooksintoaSummaryWorkbookinExcel

Conclusion
This documentation is archived and is not being maintained.
In this article, you explored several code samples that you can use to merge data from all workbooks in a folder into a master workbook. Additionally, the RDBMerge
addin can assist you to do this task very easy. Exploring and implementing these tools in your own applications can help make your job as a developer easier and make
your solutions more versatile.

Additional Resources
You can find more information on the concepts and techniques discussed in this article at the following locations.

Create a summary worksheet from all worksheets with formulas

Copy a range from closed workbooks by using ADO

Microsoft Office Developer Center

Excel Developer Portal

About the Authors


Ron de Bruin is an Excel Most Valuable Professional MVP and a frequent contributor to the newsgroups. For more information, see Ron's Excel Web page.

Frank Rice is a programming writer and frequent contributor to the Microsoft Office Developer Center.

2017 Microsoft

https://msdn.microsoft.com/enus/library/cc837974(v=office.12).aspx 21/21

You might also like