Place this code into a standard VBA module of your workbook XYZ.xls
and give it a try (you will still need to add a button that invokes
the macro, and I would suggest to let the user decide where to save it
by using the standard File | Save As command) :

Sub ConsolidateFilesIntoMasterWorkbook()

    Dim folderName As String

    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please selected the folder containing the files you
would like to consolidate."
        .Show
        If .SelectedItems.Count = 0 Then
            Exit Sub
        Else
            folderName = .SelectedItems(1)
        End If
    End With

    Dim masterWb As Workbook, sourceWb As Workbook
    Set masterWb = ActiveWorkbook

    Dim fs As Object
    Dim objFolder As Object
    Dim wbFile As Object

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fs.GetFolder(folderName)

    Application.ScreenUpdating = False

    Dim sh As Worksheet
    Dim rSource As Range, rTarget As Range

    For Each wbFile In objFolder.Files
        Set sourceWb = Workbooks.Open(wbFile.Path)
            For Each sh In sourceWb.Worksheets
                Set rSource = sh.UsedRange.Rows("2:" &
sh.UsedRange.Rows.Count)
                Set rTarget = masterWb.Sheets(1).Cells(Rows.Count,
1).End(xlUp).Offset(1)
                rSource.Copy Destination:=rTarget
            Next sh
        sourceWb.Close (False)
    Next wbFile

    Application.ScreenUpdating = True

End Sub

It is IMPORTANT that only the files you want to consolidate are in the
selected folder, and no other files!!!

Hope this helped,
Rolf

On Oct 30, 4:33 am, nitin gupta <nitin.aq...@gmail.com> wrote:
> Hi All
>
> I am an excel user with negligible knowledge if VBA codes.
> I want to know if following is possible by any generic macro:
>
> -->My objective is to consolidate few number of files (say 20 files)  where
> data is there in just  two fields i.e "Country" and "Sales in USD".   To
> clarify more: There are 20 files with 2 sheets each and each sheet have 2
> headings "Country" and "Sales in USD" with data in it . So in nutshell I
> have 40 sheets in 20 files.
> Now I want alll these data to come in One file (say XYZ.xls) one-by-one, no
> futher formatting is required
>
> ---> Above macro should be in a excel file which should have buttons to ask
> for Location  of Dumps(i.e location of 20 files)  and one more button to ask
> for Location of new file (XYZ). And this macro can be used in any machine/PC
>
> Any help on this is highly appreciated
>
> Regards
> Nitin
--~--~---------~--~----~------------~-------~--~----~
----------------------------------------------------------------------------------
Some important links for excel users:
1. Excel and VBA Tutorials(Video and Text), Free add-ins downloads at 
http://www.excelitems.com
2. Excel tutorials at http://www.excel-macros.blogspot.com
3. Learn VBA Macros at http://www.vbamacros.blogspot.com
4. Excel Tips and Tricks at http://exceldailytip.blogspot.com
 

To post to this group, send email to excel-macros@googlegroups.com
If you find any spam message in the group, please send an email to:
Ayush Jain  @ jainayus...@gmail.com or
Ashish Jain @ 26may.1...@gmail.com
<><><><><><><><><><><><><><><><><><><><><><>
HELP US GROW !!

We reach over 6,500 subscribers worldwide and receive many nice notes about the 
learning and support from the group. Our goal is to have 10,000 subscribers by 
the end of 2009. Let friends and co-workers know they can subscribe to group at 
http://groups.google.com/group/excel-macros/subscribe
-~----------~----~----~----~------~----~------~--~---

Reply via email to