Sie sind auf Seite 1von 17

The Consolidator

Introduction
In Excel 2007 Application.FileSearch doesn't work anymore. Here is a way to replace it in Excel 207 and earlier versions. This application consolidates data from any number of other workbooks which names start with "Database". You can save any number of "database" files in your directory this VBA procedure will find all of them. You can also create a directory to store any number of .xls files with all kind of names. See the VBA code on the sheet "Code". If you have questions or if you want to modify this application to suit your needs call or write: 1-613-749-4695 (Peter) peter@excel-vba.com

Your comments on this workbook are welcome: Thanks in advance Click here: peter@excel-vba.com

onsolidator

more. Here is a way to replace it in Excel 207 and earlier versions.

other workbooks which names start with "Database".

rectory this VBA procedure will find all of them.

.xls files with all kind of names.

cation to suit your needs call or write:

s on this workbook are welcome:

ere: peter@excel-vba.com

VBA Code
Code for workbooks with a constant in the name saved in a directory with many other workbooks Sub pro2007FileSearchA() Dim varPath Dim varFile Dim varThatWorkbook Dim varNbRowsIn Dim varNbRowsDatabase varPath = ThisWorkbook.Path & "\" varFile = Dir(varPath & "Database*.xls") Do While varFile <> "" Workbooks.Open varPath & varFile varThatWorkbook = ActiveWorkbook.Name Sheets("Database").Select Range("a1").Select varNbRowsIn = Selection.CurrentRegion.Rows.Count Range("A2", Range("A2").Offset(varNbRowsIn - 2, 4)).Select Selection.Copy ThisWorkbook.Activate Range("A6").Select varNbRowsDatabase = Selection.CurrentRegion.Rows.Count ActiveCell.Offset(varNbRowsDatabase, 0).Select ActiveSheet.Paste Application.CutCopyMode = False Windows(varThatWorkbook).Activate ActiveWorkbook.Close varFile = Dir Loop Range("a6").Select End Sub

In this procedure the "master database" workb and the other workbooks are in the same direc

You are looking for .xls file which name starts with Database. The asterix is a wild character.

For each file identified the data is extracted an saved in "master database"

All the data files and the master database are saved in the same directory. There are no files withut data in this directory. Sub pro2007FileSearchB() Dim varPath Dim varFile Dim varThatWorkbook Dim varNbRowsIn Dim varNbRowsDatabase varPath = "D:\Test\" 'Sheets("Database").Range("H1").Value varFile = Dir(varPath & "*.xls")

The path is either hard coded or it resides in can change it easily.

varPath = "D:\Test\" 'Sheets("Database").Range("H1").Value varFile = Dir(varPath & "*.xls") Do While varFile <> "" If varFile <> ThisWorkbook.Name Then Workbooks.Open varPath & varFile varThatWorkbook = ActiveWorkbook.Name Sheets("Database").Select Range("a1").Select varNbRowsIn = Selection.CurrentRegion.Rows.Count Range("A2", Range("A2").Offset(varNbRowsIn - 2, 4)).Select Selection.Copy ThisWorkbook.Activate Range("A6").Select varNbRowsDatabase = Selection.CurrentRegion.Rows.Count ActiveCell.Offset(varNbRowsDatabase, 0).Select ActiveSheet.Paste Application.CutCopyMode = False Windows(varThatWorkbook).Activate ActiveWorkbook.Close varFile = Dir Else varFile = Dir End If Loop Range("a6").Select End Sub You are looking through ALL .xls files. The character.

Except for "master database.xls" data is ex other files.

cedure the "master database" workbook her workbooks are in the same directory

ooking for .xls file which name starts base. The asterix is a wild character.

file identified the data is extracted and master database"

th is either hard coded or it resides in a cell where you ange it easily.

e looking through ALL .xls files. The asterix is a wild

for "master database.xls" data is extracted form all the

Master database
Consolidate Sub-Totals Date Store Product Qty 0 0.00 Dollars

Das könnte Ihnen auch gefallen